Java中的数组是在堆中分配的连续内存,它可以用来存放基本类型的值或对象类型的引用。除了操作符“[]”外,数组同样还有一个length属性,这和C++里的数组不同。
我们还可以使用多维数组:
- int a[][][] = new int[2][3][2];
- a[1][2][1] = 5;
数组内所有元素都会被赋一个默认初值(null、0、false等)。因为数组是在堆中初始化的,所以可变长度的维度是可行的。这样的数组被称为
粗糙数组:
- int a[][][] = new int[2][][];
- a[0] = new int[3][];
- a[1] = new int[5][];
- a[0][1] = new int[6];
- a[1][2] = new int[8];
- System.out.println(Arrays.deepToString(a)); // Output:
- // [[null, [0, 0, 0, 0, 0, 0], null], [null, null,
- // [0, 0, 0, 0, 0, 0, 0, 0], null, null]]
Arrays.deepToString方法可以将多维数组转成字符串。同样你也可以用数组初始化的方式来定义粗糙数组:
- int a[][][] = new int[][][] {
- { {1,2,3,4}, {5,6} },
- { {7}, {8}, {9,10,11}, {12} },
- { {13,14}, {15,16}, {17,18} }
- };
Java提供了一些对数组的操作,比如fill、equal、copy等。下面演示下:
Arrays.fill:
- void fillArray() {
- int a[] = new int[5];
- Arrays.fill(a, 3);
- System.out.println(Arrays.toString(a)); // [3, 3, 3, 3, 3]
- int b[] = new int[8];
- Arrays.fill(b, 2, 6, 3);
- System.out.println(Arrays.toString(b)); // [0, 0, 3, 3, 3, 3, 0, 0]
- }
System.arraycopy:
- void copyArray() {
- int a[] = {1, 2, 3, 4, 5};
- int b[] = new int[a.length+1];
- System.arraycopy(a, 0, b, 1, a.length);
- System.out.println(Arrays.toString(b)); // [0, 1, 2, 3, 4, 5]
- }
Arrays.equals:
- void equalsArray() {
- int a[] = {1, 2, 3, 4, 5};
- int b[] = {1, 2, 3, 4, 5};
- Arrays.equals(a, b); // true
- }
还有数组的排序,对象类型并须实现
Comparable接口才可以用
Arrays.sort排序,否则在会得到异常。示例代码:
- class MyElement implements Comparable<MyElement> {
- public int compareTo(MyElement other) {
- return this.value - other.value;
- }
- static MyElement createNext() {
- MyElement me = new MyElement();
- me.value = r.nextInt(20);
- return me;
- }
- public String toString() {
- return String.valueOf(value);
- }
- private int value;
- private static Random r = new Random();
- }
- static void sortArray() {
- MyElement meArray[] = new MyElement[8];
- for (int i = 0; i < meArray.length; ++i) {
- meArray[i] = MyElement.createNext();
- }
- System.out.println(Arrays.toString(meArray)); // [8, 18, 4, 2, 16, 0, 13, 3]
- Arrays.sort(meArray);
- System.out.println(Arrays.toString(meArray)); // [0, 2, 3, 4, 8, 13, 16, 18]
- }
如果你对Comparable的排序不满意,你可以自己定义一个
Comparator:
- class MyElementComparator implements Comparator<MyElement> {
- @Override
- public int compare(MyElement left, MyElement right) {
- return right.compareTo(left); // reverse
- }
- }
- void reverseSortArray() {
- MyElement meArray[] = new MyElement[8];
- for (int i = 0; i < meArray.length; ++i) {
- meArray[i] = MyElement.createNext();
- }
- System.out.println(Arrays.toString(meArray)); // [17, 14, 12, 9, 4, 7, 19, 11]
- Arrays.sort(meArray, new MyElementComparator()); // add a comparator
- System.out.println(Arrays.toString(meArray)); // [19, 17, 14, 12, 11, 9, 7, 4]
- }
对于排好序的数组,可以使用二分法来查找元素。
Arrays.binarySearch:
- void binarySearchArray() {
- int a[] = { 3, 5, 2, 4, 9, 7, 1 };
- Arrays.sort(a);
- int index = Arrays.binarySearch(a, 7); // a[5]=7
- }
编译器不允许你创建一个范型数组,但是允许你使用范型数组的引用:
- // List liArray[] = new List[8]; // error:
- // Cannot create a generic array of List
- List<Integer> liArray[] = new List[8]; // success!
- // liArray[0] = new ArrayList(3); // error:
- // Type mismatch: cannot convert from ArrayList to List
阅读(818) | 评论(0) | 转发(0) |