Question:
How do I convert int[] into List in Java?
Code:
import java.util.*; public class IntToList { public static void main(String[] args) { int[] ints = new int[]{1,2,3,4,5,6,7,8,9}; List<Integer> list = new ArrayList<Integer>(); for (int n = 0; n < ints.length; n++) { list.add(ints[n]); } System.out.println("int[] ints: " + Arrays.toString(ints)); System.out.print("List<Integer> list: "); for(int i=0;i<list.size();i++){ System.out.print(list.get(i) + " "); } System.out.println(); } }
Output:
$ java IntToList int[] ints: [1, 2, 3, 4, 5, 6, 7, 8, 9] Listlist: 1 2 3 4 5 6 7 8 9