将未排序的数组传递给binarysearch是一种逻辑错误。爱这种情况下,binarysearch方法返回的值是不确定的。
类Arrays提供静态方法asList。将数组当做一个List的集合。其中封装了和链表相似的行为、
asList
public static <T> List<T> asList(T... a)
- 返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同
Collection.toArray()一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了RandomAccess。此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素:
List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); -
- 参数:
-
a- 支持列表的数组。 - 返回:
- 指定数组的列表视图。
- Arrays.asList创建一个固定大小的List,他的操作速度比任何已有的List的实现都要快。但是如果对其调用add或者remove方法将会抛出异常:UnsupportedOpterationException。
而且对asList的修改,会涉及到原来的数组。如下:
- package AsList;
- import java.util.Arrays;
- import java.util.List;
- public class AsList {
- private String[] num = { "blue", "red", "yellow" };
- private int[] data = { 1, 2, 3 };
- private List list;
- private List list1;
- public AsList() {
- list = Arrays.asList(num);
- list1 = Arrays.asList(data);
- }
- public void printnumber() {
- for (int i = 0; i < list1.size(); ++i)
- System.out.println(list1.get(i));
- for (int i = 0; i < list.size(); ++i)
- System.out.println(list.get(i));
- list.set(1, "green");
- for (int i = 0; i < list.size(); ++i)
- System.out.println(list.get(i));
- for (int i = 0; i < num.length; ++i)
- System.out.println(num[i]);
- }
- public static void main(String[] args) {
- new AsList().printnumber();
- }
- }
输出结果为:
[I@c17164
blue
red
yellow
blue
green
yellow
blue
green
yellow
java.util.Arrays.asList的定义,函数参数是Varargs, 采用了泛型实现。同时由于autoboxing的支持,使得可以支持对象数组以及基本类型数组。
不过在使用时,当传入基本数据类型的数组时,会出现小问题,会把传入的数组整个当作返回的List中的第一个元素,例如:
- 1 public static void main(String[] args){
- 2 int[] a1 = new int[]{1,2,3};
- 3 String[] a2 = new String[]{"a","b","c"};
- 4
- 5 System.out.println(Arrays.asList(a1));
- 6 System.out.println(Arrays.asList(a2));
-
7 }
打印结果如下:
[[I@dc8569]
[a, b, c]下面说说Arrays.asList()的返回值:
JDK文档是这么说的:public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素: List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");我们都知道,List的一个典型的特性就是其长度是可变的,我们可以很方便地对它进行插入和删除元素的操作,这是它与数组所存在的一个很大的区别,后者的长度是固定的,而且我们不能从数组中删除元素,只能修改元素的值。利用Arrays.asList(array)将返回一个List,然而这个返回的List并不支持add和remove的操作。
这是什么原因呢?
Arrays.asList源码:
1 public static <T> List<T> asList(T... a) {
2 return new ArrayList<T>(a);
3 }这里的ArrayList并不是java.util.ArrayList,而是Arrays的内部类:
1 /**
2 * @serial include
3 */
4 private static class ArrayList<E> extends AbstractList<E> implements
5 RandomAccess, java.io.Serializable {
6 private static final long serialVersionUID = -2764017481108945198L;
7 private final E[] a;
8
9 ArrayList(E[] array) {
10 if (array == null)
11 throw new NullPointerException();
12 a = array;
13 }
14
15 public int size() {
16 return a.length;
17 }
18
19 public Object[] toArray() {
20 return a.clone();
21 }
22
23 public <T> T[] toArray(T[] a) {
24 int size = size();
25 if (a.length < size)
26 return Arrays.copyOf(this.a, size, (Class<? extends T[]>) a
27 .getClass());
28 System.arraycopy(this.a, 0, a, 0, size);
29 if (a.length > size)
30 a[size] = null;
31 return a;
32 }
33
34 public E get(int index) {
35 return a[index];
36 }
37
38 public E set(int index, E element) {
39 E oldValue = a[index];
40 a[index] = element;
41 return oldValue;
42 }
43
44 public int indexOf(Object o) {
45 if (o == null) {
46 for (int i = 0; i < a.length; i++)
47 if (a[i] == null)
48 return i;
49 } else {
50 for (int i = 0; i < a.length; i++)
51 if (o.equals(a[i]))
52 return i;
53 }
54 return -1;
55 }
56
57 public boolean contains(Object o) {
58 return indexOf(o) != -1;
59 }
60 }我们可以看到该内部类继承的是AbstractList,下面是AbstractList的add和remove方法源码:
1 public boolean add(E e) {
2 add(size(), e);
3 return true;
4 }
5
6 public void add(int index, E element) {
7 throw new UnsupportedOperationException();
8 }
9
10 public E remove(int index) {
11 throw new UnsupportedOperationException();
12 }所以,当我们对Arrays.asList返回的List进行添加或删除时将会报 java.lang.UnsupportedOperationException 异常。
打印结果如下:
[[I@dc8569]
[a, b, c]
下面说说Arrays.asList()的返回值:
public static <T> List<T> asList(T... a) 返回一个受指定数组支持的固定大小的列表。(对返回列表的更改会“直接写”到数组。)此方法同 Collection.toArray() 一起,充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。返回的列表是可序列化的,并且实现了 RandomAccess。此方法还提供了一个创建固定长度的列表的便捷方法,该列表被初始化为包含多个元素: List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
我们都知道,List的一个典型的特性就是其长度是可变的,我们可以很方便地对它进行插入和删除元素的操作,这是它与数组所存在的一个很大的区别,后者的长度是固定的,而且我们不能从数组中删除元素,只能修改元素的值。利用Arrays.asList(array)将返回一个List,然而这个返回的List并不支持add和remove的操作。
这是什么原因呢?
Arrays.asList源码:
1 public static <T> List<T> asList(T... a) {
2 return new ArrayList<T>(a);
3 }
这里的ArrayList并不是java.util.ArrayList,而是Arrays的内部类:
1 /**
2 * @serial include
3 */
4 private static class ArrayList<E> extends AbstractList<E> implements
5 RandomAccess, java.io.Serializable {
6 private static final long serialVersionUID = -2764017481108945198L;
7 private final E[] a;
8
9 ArrayList(E[] array) {
10 if (array == null)
11 throw new NullPointerException();
12 a = array;
13 }
14
15 public int size() {
16 return a.length;
17 }
18
19 public Object[] toArray() {
20 return a.clone();
21 }
22
23 public <T> T[] toArray(T[] a) {
24 int size = size();
25 if (a.length < size)
26 return Arrays.copyOf(this.a, size, (Class<? extends T[]>) a
27 .getClass());
28 System.arraycopy(this.a, 0, a, 0, size);
29 if (a.length > size)
30 a[size] = null;
31 return a;
32 }
33
34 public E get(int index) {
35 return a[index];
36 }
37
38 public E set(int index, E element) {
39 E oldValue = a[index];
40 a[index] = element;
41 return oldValue;
42 }
43
44 public int indexOf(Object o) {
45 if (o == null) {
46 for (int i = 0; i < a.length; i++)
47 if (a[i] == null)
48 return i;
49 } else {
50 for (int i = 0; i < a.length; i++)
51 if (o.equals(a[i]))
52 return i;
53 }
54 return -1;
55 }
56
57 public boolean contains(Object o) {
58 return indexOf(o) != -1;
59 }
60 }
我们可以看到该内部类继承的是AbstractList,下面是AbstractList的add和remove方法源码:
1 public boolean add(E e) {
2 add(size(), e);
3 return true;
4 }
5
6 public void add(int index, E element) {
7 throw new UnsupportedOperationException();
8 }
9
10 public E remove(int index) {
11 throw new UnsupportedOperationException();
12 }
所以,当我们对Arrays.asList返回的List进行添加或删除时将会报 java.lang.UnsupportedOperationException 异常。