在 java.util 中有一个叫做 Arrays 的类。这个类提供了各种在进行数组运算时很有用的方法。尽管这些方法在技术上不属于集合框架,但他们提供了跨越类集合数组的桥梁。 Arrays 类定了以 5 中类型的方法。
asList():返回一个被数组支持的 List 。binarySearch() :在不同类型的数组中搜素特定值。equals():比较两个数组是否相等。fill():用一个指定的值填充数组。sort():对不同类型的数组排序。Arrays 类的使用:
import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class ArrayListDemo { public static void main(String[] args) { Integer array[] = new Integer[9]; for (int i = 1; i < 10; i++){ array[i - 1] = new Integer(-3 * i); } System.out.print("original contens: " ); display(array); Arrays.sort(array); System.out.print("Sorted :"); display(array); Arrays.fill(array,0,3,-1); System.out.print("After fill():"); display(array); Arrays.sort(array); System.out.print("After sorting again:"); display(array); System.out.print("the value -9 is at location: "); int index = Arrays.binarySearch(array, -9); System.out.println(index); List l = Arrays.asList(array); System.out.println("ths size of list: " + l.size()); System.out.println("the contens fo list:"); for (ListIterator i = l.listIterator(); i.hasNext();){ System.out.print( i.next() + " "); } } static void display(Integer array[]){ for ( int i = 0; i < array.length; i++){ System.out.print(array[i] + " "); } System.out.println(""); } } // original contens: -3 -6 -9 -12 -15 -18 -21 -24 -27 // Sorted :-27 -24 -21 -18 -15 -12 -9 -6 -3 // After fill():-1 -1 -1 -18 -15 -12 -9 -6 -3 // After sorting again:-18 -15 -12 -9 -6 -3 -1 -1 -1 // the value -9 is at location: 3 // ths size of list: 9 // the contens fo list: // -18 -15 -12 -9 -6 -3 -1 -1 -1