Java Toarray
import java.util.*;
public class Scaler {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
//Implementing the first way to convert list to array in java
Object[] res1Arr=list.toArray();
//Implementing the second way to convert list to array in java
Integer[] res2Arr = new Integer[4];
list.toArray(res2Arr);
//Printing the first array
System.out.println("Printing 'res1Arr':");
for(Object obj: res1Arr)
//Typecasting required for the first method
System.out.println((Integer)obj);
//Printing the second array
System.out.println("Printing 'res2Arr':");
for(Integer i: res2Arr)
System.out.println(i);
}
}
codelearner