Friends who are engaged in R&D know that in project development, you often encounter mutual conversion between list and array types. This article explains the conversion process to you through a simple example.
Java code
package test.test1; import java.util.ArrayList; import java.util.List; public class Test { /** * @param args */ public static void main(String[] ar gs) { List list=new ArrayList() ; list.add("Wang Lihu"); list.add("Zhang San"); list.add("Li Si"); 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 ArrayList type data to String[], you must traverse the List type. In fact, there is no need. List provides us with a good method to solve the problem of List conversion into an array. , look at another example:
package test.test1; import java.util.ArrayList; import java.util.List;
Java code
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("Li Si"); 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]); } } }
Have you found that this is what you want? In fact, it is very simple. ArrayList provides public <T> T[] toArray(T[] a) method to return an array containing all elements in this list in the correct order; the runtime type of the return array is the runtime type of the specified array. If the list can be placed into the specified array, the array to be placed into the element of this list is returned. Otherwise, a new array is assigned based on the runtime type of the specified array and the size of this list.
If the specified array can accommodate a list and have room left (i.e., the array has more elements than the list), the element in the array that follows the end of the set is set to null. This is useful for determining the length of the list, but only if the caller knows that the list does not contain any null elements.
So how do you convert an array into a List? Take a look at a small example, as shown below:
Java code
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]="Li Si"; 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 troublesome? In fact, the problem of converting arrays into List Arrays object also provides us with public static <T> List<T> asList(T... a) for us to call, try running 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=ne w String[3]; array[0]= "Wang Lihu"; array[1]="Zhang San"; array[2]="Li Si"; List<String> list=Arrays.asList(array); for(int i=0;i<list.size( );i++){ System.out.println(list.get(i)); } } }
To be simple, the asList method returns a fixed-size list supported by a specified array. This method, together with Collection.toArray, acts as a bridge between an array-based API and a collection-based API. The returned list is serializable and implements 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 3","Li Si"); for(int i=0;i<list.size();i++){ System.out.println(list.get(i)); } } }