How to Convert an Array to a List in Java
Ordinary arrays are the first data structure every new Java programmer learns about, but they are not the best for every situation. The "List" collection of data types, which includes both the ArrayList and the LinkedList classes, provides a way to efficiently resize their collections without reinitializing the entire data structure, an ability sorely missing from ordinary Java arrays. Fortunately, Java provides a helper method for converting an existing array into a list.
Instructions
-
-
1
Open "Netbeans" or your favorite Integrated Development Environment.
-
2
Click "File" and "New Class." Name it "ListCreator."
-
-
3
Type "psvm". Netbeans will expand it automatically into a valid "main" method.
-
4
Type the following to declare an array:
Integer[] myArray = {1,3,4,5,6,23,4,4};
-
5
Add the following line to convert the array to a list:
List list = Arrays.asList(myArray);
-
6
Click "Run."
-
1