1.List conversion into an array. (The list here is the entity is ArrayList)
Invokes the ArrayList ToArray method.
ToArray
Public t[] ToArray (t[] a) returns an array that contains all the elements in this list in the correct order; The Run-time type of the array is the Run-time type of the specified array. If the list can be placed in the specified array, the array that is placed in this list element is returned. Otherwise, a new array is allocated based on the Run-time type of the specified array and the size of the list.
If the specified array can hold the list and have the remaining space (that is, the elements of the array are more than the list), the element that is immediately at the end of the collection is set to NULL in the array. This is useful for determining the length of a list, but only when callers know that the list does not contain any null elements.
Designated by:
ToArray in the interface Collection
Designated by:
ToArray in the interface list
Covered:
ToArray in the class Abstractcollection
Parameters:
A-to store an array of list elements, if it is large enough; otherwise, it is a new array of the same run-time type allocated for storing list elements.
Return:
An array that contains the elements of the list.
Thrown:
Arraystoreexception-If A's run-time type is not a super type of the run-time type for each element in this list.
Specific usage:
Copy Code code as follows:
List List = new ArrayList ();
List.add ("1");
List.add ("2");
final int size = List.size ();
String[] arr = (string[]) List.toarray (new string[size));
2. Array conversion into List
Invokes the arrays Aslist method.
JDK 1.4 defines the java.util.Arrays.asList, and the function argument is object[]. Therefore, aslist () in 1.4 does not support primitive types of arrays as parameters.
In JDK 1.5, the definition of java.util.Arrays.asList, the function parameter is varargs, adopts the generic implementation. At the same time, because of autoboxing support, object arrays and primitive type arrays can be supported.
However, when used, when passing in an array of basic data types, there is a small problem, which treats the incoming array as the first element in the returned list, for example:
Copy Code code as follows:
public static void Main (string[] args) {
int[] a1 = new int[]{1,2,3};
string[] A2 = new string[]{"A", "B", "C"};
System.out.println (arrays.aslist (A1));
System.out.println (Arrays.aslist (A2));
}
1. The printed results are as follows:
Copy Code code as follows:
1
2 [[i@dc8569]
[A, B, c]
Let's say the return value of Arrays.aslist ():
This is what the JDK documentation says:
public static <T> list<t> aslist (T. A) returns a fixed size list supported by the specified array. (Changes to the return list are "written directly" to the array.) This method, together with Collection.toarray (), serves as a bridge between an array based API and a Collection API. The returned list is serializable and implements the randomaccess. This method also provides a convenient way to create a fixed-length list that
Copy Code code as follows:
/**
* @serial include
*/
private static Class Arraylist<e>extends abstractlist<e>implements randomaccess, java.io.Serializable {
Private static final long serialversionuid = -2764017481108945198l;
Private final e[] A;
ArrayList (e[] array) {
if (array = = NULL)
throw new NullPointerException ();
A = array;
}
public int size () {
return a.length;
}
Public object[] ToArray () {
return A.clone ();
}
Public <T> t[] ToArray (t[] a) {
int size = size ();
if (A.length < size)
Return arrays.copyof (THIS.A, size, (class<? extends t[]>) A.getclass ());
System.arraycopy (this.a,0, a, 0, size);
if (A.length > Size)
A[size] = null;
return A;
}
Public E get (int index) {
return A[index];
}
Public E Set (int index, E element) {
E oldValue = A[index];
A[index] = element;
return oldValue;
}
public int indexOf (Object o) {
if (o = = null) {
for (int i = 0; i < a.length; i++)
if (a[i] = = null)
return i;
}else {
for (int i = 0; i < a.length; i++)
if (O.equals (A[i]))
return i;
}
return-1;
}
Public Boolean contains (Object o) {
return IndexOf (O)!=-1;
}
}
The table is initialized to contain multiple elements: list<string> stooges = arrays.aslist ("Larry", "Moe", "Curly");
As we all know, a typical feature of list is that its length is variable, we can easily insert and delete elements of it, which is a big difference from the array, the length is fixed, and we can not remove elements from the array, can only modify the value of the element. Using Arrays.aslist (array) returns a list, but the returned list does not support the actions of add and remove.
What's the reason?
Arrays.aslist Source:
Copy Code code as follows:
1
2
3 public static <T> list<t> aslist (T. a) {
return new Arraylist<t> (a);
}
The ArrayList here are not java.util.ArrayList, but arrays's inner classes:
We can see that the inner class inherits the Abstractlist, and the following is the Abstractlist add and remove method source:
Copy Code code as follows:
Public boolean Add (E e) {
Add (Size (), e);
return true;
}
public void Add (int index, E element) {
throw new Unsupportedoperationexception ();
}
Public E-Remove (int index) {
throw new Unsupportedoperationexception ();
}
Therefore, when we add or delete the list returned by Arrays.aslist, we will report an java.lang.UnsupportedOperationException exception.