我该如何缩短它?
int[] a1 = {2, 0, 1, 4, 3};
int[] a2 = {3, 1, 2, 0, 4};
int[] a3 = {4, 2, 3, 1, 0};
int[] a4 = {0, 3, 4, 2, 1};
int[] a5 = {1, 4, 0, 3, 2};
ArrayList<int[]> array = new ArrayList<int[]>();
array.add(a1);
array.add(a2);
array.add(a3);
array.add(a4);
array.add(a5);
最佳答案
List<int[]> ints = Arrays.asList(new int[][]{{2, 0, 1, 4, 3},
{3, 1, 2, 0, 4},
{4, 2, 3, 1, 0},
{0, 3, 4, 2, 1},
{1, 4, 0, 3, 2}});
是一种方法。
编辑:bmargulies 正确地指出,生成的 List
不一定是 ArrayList
;如果需要,您可以将元素复制到新元素中。
关于java - 高效添加 ArrayList 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2090417/