Java List to Array

Java List to Array

Convert List to Array

List<String> list = new ArrayList<>();
list.add("man");
list.add("women");

String[] arr = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arr));

Convert Array to List

String[] arr = {"car","bike","van"};

List<String> list = Arrays.asList(arr); // return fixed size list

System.out.println(Arrays.toString(arr));
System.out.println(list);

System.out.println(list.size()); // 3
System.out.println(arr.length); // 3

Replace element

list.set(0, "lori")
System.out.println(Arrays.toString(arr));
System.out.println(list);

Add new element to fixed size list and array

list.add("airplane") // java.lang.UnsupportedOperationException



Write a Reply or Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.