package xiaoling; import java.util.Arrays; import java.util.ArrayList; import java.util.List; public class ListTest{ public static void main(String[] args){ List<List<Integer>> list = new ArrayList<>(); for (int num=0; num<10; ++num){ list.add(new ArrayList(Arrays.asList(num, num+1))); } System.out.println(list); } }
运行结果为:[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10]]
数组测试:
1 package xiaoling; 2 import java.util.Arrays; 3 import java.util.ArrayList; 4 import java.util.List; 5 public class ListTest{ 6 public static void main(String[] args){ 7 List<List<Integer>> list = new ArrayList<>(); 8 for (int num=0; num<5; ++num){ 9 list.add(new ArrayList(Arrays.asList(num, num+1))); 10 } 11 System.out.println(list); 12 13 int[] nums = new int[]{11, 12, 13, 14, 15}; 14 list.add(new ArrayList(Arrays.asList(nums))); 15 System.out.println(list); 16 17 Integer[] nums1 = new Integer[]{11, 12, 13, 14, 15}; 18 list.add(new ArrayList(Arrays.asList(nums1))); 19 System.out.println(list); 20 } 21 }
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [[I@4defb0be]]
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [[I@4defb0be], [11, 12, 13, 14, 15]]