主题:【原创】Java 1.5新特点的一些体验 -- 老兵帅客
Arrays并不是在Java 1.5中新出现的,但是Java 1.5确实给它增加了很多Utility Methods,下面是一个简单的列表和说明:
1。deepEquals:Returns true if the two specified arrays are deeply equal to one another.Two array references are considered deeply equal if both are null, or if they refer to arrays that contain the same number of elements and all corresponding pairs of elements in the two arrays are deeply equal.
2。deepHashCode:Returns a hash code based on the "deep contents" of the specified array. If the array contains other arrays as elements, the hash code is based on their contents and so on, ad infinitum. For any two arrays a and b such that Arrays.deepEquals(a, b), it is also the case that Arrays.deepHashCode(a) == Arrays.deepHashCode(b).
3。deepToString:Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.
4。toString:Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(short). Returns "null" if a is null.
评论:上面四个方法中前三个都属于deep系列,它们的主要用处是处理多位数组下的判等、Hash和字符串化,但是说明文字中没有说明如果出现数组元素的递归自访问将如何处理;toString应该说是对以前版本缺陷的一个补偿。
下面是一个简单的例子,说明了该类不同方法的一些使用:
package test2005_2;
import java.util.Arrays;
import java.util.List;
public class ArraysTester {
private int[] ar;
public ArraysTester(int numValues) {
ar = new int[numValues];
for (int i = 0; i < ar.length; i++) {
ar[i] = (1000 - (300 + i));
}
}
public int[] get() {
return ar;
}
public static void main(String[] args) {
ArraysTester tester = new ArraysTester(50);
int[] myArray = tester.get();
// Compare two arrays
int[] myOtherArray = tester.get().clone();
if (Arrays.equals(myArray, myOtherArray)) {
System.out.println("The two arrays are equal!");
} else {
System.out.println("The two arrays are not equal!");
}
// Fill up some values
Arrays.fill(myOtherArray, 2, 10, new Double(Math.PI).intValue());
myArray[30] = 98;
// Print array, as is
System.out.println("Here's the unsorted array...");
System.out.println(Arrays.toString(myArray));
System.out.println();
// Sort the array
Arrays.sort(myArray);
// print array, sorted
System.out.println("Here's the sorted array...");
System.out.println(Arrays.toString(myArray));
System.out.println();
// Get the index of a particular value
int index = Arrays.binarySearch(myArray, 98);
System.out.println("98 is located in the array at index " + index);
String[][] ticTacToe = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}
};
System.out.println(Arrays.deepToString(ticTacToe));
String[][] ticTacToe2 = { {"O", "O", "X"},
{"O", "X", "X"},
{"X", "O", "X"}
};
String[][] ticTacToe3 = { {"X", "O", "O"},
{"O", "X", "X"},
{"X", "O", "X"}
};
if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
System.out.println("Boards 1 and 2 are equal.");
} else {
System.out.println("Boards 1 and 2 are not equal.");
}
if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
System.out.println("Boards 1 and 3 are equal.");
} else {
System.out.println("Boards 1 and 3 are not equal.");
}
}
}
- 相关回复 上下关系6
1.3 Ordering Queues Using Comparators 老兵帅客 字1658 2004-10-01 16:46:18
.NET中有类似的东西。由于.NET使用delegate,而不是 Highway 字57 2004-10-01 16:57:05
1.2 Using Queues 老兵帅客 字953 2004-10-01 16:33:01
1.1 Working with Arrays
😄热烈欢迎! Highway 字0 2004-10-01 16:01:22
😄虽然俺完全不懂,但是看到这样真诚尽现的技术帖,一定要跟着版主叫个好,大力支持 龙城飞将 字0 2004-10-02 14:38:50