Convert Array to List and List to Array - DV's Coding Corner
 
import java.util.*;
public class array_list {
public static void main(String args[]){


//Array to List
Integer a[]=new Integer[]{1,2,3};
List<Integer> array2list=new ArrayList<Integer>();
array2list=Arrays.asList(a);  
// OR 
// List<Integer> array2list=Arrays.asList(a);
System.out.println(array2list);

//List to Array
List<String> ls=new ArrayList<String>();
ls.add("a");
ls.add("b");
String[] list2array=ls.toArray(new String[ls.size()]);
// OR // String[] list2array=ls.toArray(new String[0]);
System.out.println(Arrays.toString(list2array));

}
}
Output:
[1, 2, 3]
[a, b]




Leave a Reply.