Java ArrayList arrays convert to each other

  • 2020-04-01 04:23:58
  • OfStack

Do r & d friends know that in the project development, often encountered between the list and array types of mutual conversion, this article through a simple example to explain the conversion process.  

Java code


package test.test1; 
import java.util.ArrayList; 
import java.util.List; 
public class Test { 
   
  public static void main(String[] args) { 
    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 converting ArrayList data to String[], we have to iterate over the List type. In fact, it is not necessary.


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]); 
    } 
  } 
} 

Do you find that this is what you want? Simply put, ArrayList provides public < T> The T[] toArray(T[] a) method returns an array containing all the elements in the list in the correct order. The runtime type of the returned array is the runtime type of the specified array. If the list can fit into the specified array, returns the array of elements that fit into the list. Otherwise, a new array is assigned 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 space left (that is, there are more elements in the array than in the list), then the element immediately following the collection in the array 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 to a List? Let's take another small example, as follows:

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 realize it's a lot of work? In fact, the Arrays object converted into a List also provides us with public static < T> List< T> AsList (T... A) for our call, try 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]=" Li si "; 
    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 a specified array, which, along with collection.toarray, ACTS as a bridge between the array-based API and the collection-based API. The returned list is serializable, and RandomAccess is implemented. 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 "," Li si "); 
    for(int i=0;i<list.size();i++){ 
      System.out.println(list.get(i)); 
    } 
  } 
} 


Related articles: