Converting Array to List in java
asList()
method that helps to convert array to List. This method copies an array into List. When we use asList()
method, the array and list both shares the same heap space. When update
is made to the list, it reflects the changes to array and vice-versa.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Arrays; import java.util.List; public class ArrayToList { public static void main(String[] args) { String[] names = { "Neel", "Clay", "Adams", "Joseph", "Jack" }; // Converting Array to List List aList = Arrays.asList(names); System.out.println("Size of list=" + aList.size()); System.out.println("aList Values=" + aList); //changing the aList values aList.set(2, "Dinesh"); // printing aList values for (String name : names) { System.out.println(name); } } } |
Output
Comments
Post a Comment