Reprint: http://blog.csdn.net/brave_heart_lxl/article/details/6178909
List-----> Arrays
In development, we inevitably encounter the conversion between list and array type, and give a simple example:
Package test.test1;
Import java.util.ArrayList;
Import java.util.List;
public class Test {
/**
* @param args
*/
public static void Main (string[] args) {
List list=new ArrayList ();
List.add ("Wang Lihu");
List.add ("Zhang San");
List.add ("John Doe");
int size=list.size ();
String[] Array=new string[size];
for (int i=0;i<list.size (); i++) {
array[i]= (String) list.get (i);
}
for (int i=0;i<array.length;i++) {
System.out.println (Array[i]);
}
}
}
As listed above, when you want to convert the ArrayList type of data to string[], the list type must be traversed, in fact, there is no such necessity, the list provides us a good way to solve the problem of the list conversion into an array, not to see an example:
Package test.test1;
Import java.util.ArrayList;
Import java.util.List;
public class Test {
public static void Main (string[] args) {
List<string> list=new arraylist<string> ();
List.add ("Wang Lihu");
List.add ("Zhang San");
List.add ("John Doe");
int size=list.size ();
string[] Array = (string[]) List.toarray (new string[size]);
for (int i=0;i<array.length;i++) {
System.out.println (Array[i]);
}
}
}
Did you find out that this is what you want? In fact, ArrayList provides public <T> t[] ToArray (t[] A) method returns an array that contains all the elements in this list in the correct order; The run-time type of the returned array is the run-time type of the specified array. If the list can fit into 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 has the remaining space (that is, the array has more elements than the list), the element immediately following the collection at the end of the array is set to NULL. This is useful for determining the length of a list, but only if the caller knows that the list does not contain any null elements.
Array--->list
So how do you convert an array to a list? Do not look at a small example, as follows:
Package test.test1;
Import java.util.ArrayList;
Import java.util.List;
public class Test {
public static void Main (string[] args) {
String[] Array=new string[3];
array[0]= "Wang Lihu";
Array[1]= "Zhang San";
array[2]= "John Doe";
List<string> list=new arraylist<string> ();
for (int i=0;i<array.length;i++) {
List.add (Array[i]);
}
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}
}
}
Don't you find it a lot of trouble? In fact, the problem of array conversion to List arrays object is also provided to US public static <T> list<t> aslist (T ... a) for our call, try to run the following example:
Package test.test1;
Import Java.util.Arrays;
Import java.util.List;
public class Test {
public static void Main (string[] args) {
String[] Array=new string[3];
array[0]= "Wang Lihu";
Array[1]= "Zhang San";
array[2]= "John Doe";
list<string> list=arrays.aslist (array);
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}
}
}
Simply put, the Aslist method returns a fixed-size list supported by the specified array, which, together with Collection.toarray, serves as a bridge between the array-based API and the Collection-based API. The returned list is serializable and implements the randomaccess. In addition, this method provides a convenient way to create a fixed-length list that is initialized to contain multiple elements:
Package test.test1;
Import Java.util.Arrays;
Import java.util.List;
public class Test1 {
public static void Main (string[] args) {
list<string> list = arrays.aslist ("Wang Lihu", "Zhang San", "John Doe");
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}
}
}
Conversions between ArrayList and arrays