Convert Array to List in Java.
This example shows how to convert an Array to a List in Java using Arrays asList method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.*; import java.io.*; public class ArrayToCollection { public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("How many elements you want to add to the array: "); int n = Integer.parseInt( in .readLine()); String[] name = new String[n]; for (int i = 0; i < n; i++) { name[i] = in .readLine(); } List list = Arrays.asList(name); System.out.println(); for (String li: list) { String str = li; System.out.print(str + " "); } } } |
Output:
How many elements you want to add to the array:
red white green
red white green
red white green