2、数组(数据结构)

来源:互联网 发布:美国网络恐怖组织 编辑:程序博客网 时间:2024/05/29 11:10

1、数组(Array)是什么?

答: 数组是应用最多的数据存储结构(指在内存或磁盘中的存储方式),所有的编程语言都有数组,即数组是一种数据结构、也是java中的一种引用数据类型。注意哈,是数据存储结构哦,注意与数据逻辑结构的区分!


2、数组在java中的应用?

答:C++中数组是作为基本数据类型的,而java中,数组是作为引用数据类型(也称对象类型),所以创建一个数组对象,我们还要用到new哦。new 一个 数组实例对象。这里语法就不再叙述了。


3、上个例子,让我们看看数组的增删查,面向过程版本。

class ArrayApp {public static void main(String args[]) {long[] arr = new long[100]; // 一个数组实例对象int mEles = 0; // 当前元素数量long searchKey = 0; // 要查找的值int j = 0; //用于for循环arr[0] = 77; // insert 增加arr[1] = 99;arr[2] = 44;arr[3] = 55;arr[4] = 22;arr[5] = 88;arr[6] = 11;arr[7] = 9;arr[8] = 102;arr[9] = 33;mEles = 10; // 插入了10个元素//…………………………………………………………………………………………………………………………………………………………for (int i = 0; i < mEles; i++) { // 显示System.out.print(arr[i] + " ");}System.out.println(); // 换行//…………………………………………………………………………………………………………………………………………………………searchKey = 33; //查找算法开始 for (j = 0; j < mEles; j++) {if (arr[j] == searchKey) {break;}}if(j == mEles) {System.out.println("没有这个值啊,兄弟");} else {System.out.println("找到你的值了" + searchKey);}//…………………………………………………………………………………………………………………………………………………………searchKey = 55;   //开始删除算法for(j = 0; j < mEles; j++) {  //找到keyif(arr[j] == searchKey) {break;}}for(int k = j;k < mEles; k++) {  //删除一个后,后面的元素要向前移动arr[k] = arr[k + 1];}mEles = mEles -1;  //元素数量减去一个//…………………………………………………………………………………………………………………………………………………………for(j = 0; j < mEles; j++) {    //再看一下System.out.print(arr[j] + " ");}}}


4、上面同样的例子,同面向对象的思想,用两个类来实现,一个类的实例对象用于存储元素,一个类作为main方法入口

public class LowArray {private long[] array = null; //一个数组引用对象作为实例变量private int ele_num; //当前插入元素数量int j;//用于标记当前索引值public LowArray(int size) {array = new long[size];}public void insert(long num) {  //插入接口array[ele_num] = num;ele_num++;}public void display() {for(int i = 0; i < ele_num; i++) {System.out.print(array[i] + " ");}System.out.println();}public int getCurrentNum() {return ele_num;}public boolean query(long num) {for(j = 0; j < ele_num; j++) { //查找num是否存在if( array[j] == num) {break;}}if( j == ele_num) {System.out.println("你找的那个值不存在啊,哥");return false; //直接退出整个方法} else {System.out.println("找到那个值了" + num);return true;}}public void delete(long num) {if(query(num)) {for(int k = j; k < ele_num; k++) { //后面的元素往前移array[k] = array[k + 1]; }ele_num--;//当前元素减少1个}}}

class Main {public static void main(String args[]) {LowArray temp = new LowArray(10);temp.insert(1);temp.insert(2);temp.insert(3);temp.insert(4);temp.display();temp.delete(2);temp.display();}}


5、有序数组,只要你的数组元素是有序的,那么查询(query)就要爽了,可以使用二分查找


6、总结
a、Java中的数组,本身就是个实例对象哦,由new 在内存中创建。

b、无序数组插入非常块(因为都是在索引值尾部嘛), 查询和删除比较慢(查询要遍历所有元素、删除要做向前移动填充(所谓的删除也是释放内存空间啊,既然是数组必须是连续的))

c、将数组封装在一个类中,作为private实例变量,可以有效保护数组

d、类的接口,由public方法组成(这算废话吗?)

e、类的接口,让别人可以用的最爽为妙,封装的好好的

f、有序的数组,可以使用二分查找 


7、有序数组,二分查找算法java实现嘿嘿

public class OrderTwo {private int[] numbers = { 1, 6, 7, 9, 10, 43, 59, 69, 80, 99, 105, 199, 503 };// 有序数组public static void main(String args[]) {OrderTwo orderTwo = new OrderTwo();// System.out.println(orderTwo.find(199));System.out.println(orderTwo.searchNow(199));}public int searchNow(int searchKey) {int low = 0;int high = numbers.length - 1;while (low <= high) {int middle = (low + high) / 2;if (searchKey == numbers[middle]) {return middle;} else if (searchKey < numbers[middle]) {high = middle - 1;} else if (searchKey > numbers[middle]) {low = middle + 1;}}return -1;}}


8、效率极低的线程查找,从索引0开始遍历呗,嘿嘿

public int findNow(int searchKey) {int high = numbers.length;int index = 0;while(index < high) {if(searchKey == numbers[index]) {return index;} else {index++;}}return -1;}


9、为什么不用数组存储一切对象?

答:

无序数组缺点:搜索非常之慢,因为你要一个一个全部遍历。
无序数组优点:插入非常快,有了对应的索引值,插入很快。




0 0