目录
java.util.Arrays 类是 JDK 提供的一个工具类,用来处理数组的各种方法,而且每个方法基本上都是静态方法,能直接通过类名Arrays调用。
1、asList
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
作用是返回由指定数组支持的固定大小列表。
注意:这个方法返回的 ArrayList 不是我们常用的集合类 java.util.ArrayList。这里的 ArrayList 是 Arrays 的一个内部类 java.util.Arrays.ArrayList。这个内部类有如下属性和方法:



1 private static class ArrayList<E> extends AbstractList<E>
2 implements RandomAccess, java.io.Serializable
3 {
4 private static final long serialVersionUID = -2764017481108945198L;
5 private final E[] a;
6
7 ArrayList(E[] array) {
8 if (array==null)
9 throw new NullPointerException();
10 a = array;
11 }
12
13 public int size() {
14 return a.length;
15 }
16
17 public Object[] toArray() {
18 return a.clone();
19 }
20
21 public <T> T[] toArray(T[] a) {
22 int size = size();
23 if (a.length < size)
24 return Arrays.copyOf(this.a, size,
25 (Class<? extends T[]>) a.getClass());
26 System.arraycopy(this.a, 0, a, 0, size);
27 if (a.length > size)
28 a[size] = null;
29 return a;
30 }
31
32 public E get(int index) {
33 return a[index];
34 }
35
36 public E set(int index, E element) {
37 E oldValue = a[index];
38 a[index] = element;
39 return oldValue;
40 }
41
42 public int indexOf(Object o) {
43 if (o==null) {
44 for (int i=0; i<a.length; i++)
45 if (a[i]==null)
46 return i;
47 } else {
48 for (int i=0; i<a.length; i++)
49 if (o.e

本文详细介绍了JDK1.8中java.util.Arrays类的主要方法,包括asList、sort、binarySearch、copyOf、equals和deepEquals、fill以及toString和deepToString的使用和原理。asList返回的列表不允许增删操作,sort使用了快速排序算法,binarySearch依赖于数组的有序性,copyOf用于数组拷贝,equals和deepEquals比较数组元素,fill用于数组赋值,toString和deepToString则分别用于一维和多维数组的打印。
最低0.47元/天 解锁文章
203

被折叠的 条评论
为什么被折叠?



