1List<String> list = new ArrayList<>();
2list.add("a");
3list.add("ab");
4list.add("abc");
5list.add("abcd");
6// convert
7String[] array = list.toArray();
1import java.util.ArrayList;
2public class ListToArray {
3 public static void main(String args[]){
4 ArrayList<String> list = new ArrayList<String>();
5 list.add("Apple");
6 list.add("Orange");
7 list.add("Banana");
8
9 System.out.println("Contents of list ::"+list);
10 String[] myArray = new String[list.size()];
11 list.toArray(myArray);
12
13 for(int i=0; i<myArray.length; i++){
14 System.out.println("Element at the index "+i+" is ::"+myArray[i]);
15 }
16 }
17}
1Integer[] arr = new Integer[al.size()];
2
3 // ArrayList to Array Conversion
4 for (int i = 0; i < al.size(); i++)
5 arr[i] = al.get(i);
1Integer[] arr = new Integer[al.size()];
2for (int i = 0; i < al.size(); i++)
3 arr[i] = al.get(i);