Write a Java program to convert an array to an ArrayList, and an ArrayList to an array.
public static void main(String[] args) {
Integer[] array = {1, 2, 3, 3, 4};
//Array to ArrayList
List<Integer> list = Arrays.asList(array);
for (Integer x : list)
System.out.print(x + " ");
System.out.println();
//ArrayList to array
Integer[] arr = new Integer[list.size()];
arr = list.toArray(arr);
for (Integer x : arr)
System.out.print(x + " ");
}
Comments
Leave a comment