2016-03-03 196 views
2

我有这样一段代码:将二维数组转换为二维数组列表?

int[][] pattern = new int[][]{ 
     { 1, 1, 1, 1, 1, 1, 1 }, 
     { 1, 2, 0, 0, 0, 2, 1 }, 
     { 1, 0, 3, 0, 3, 0, 1 }, 
     { 1, 0, 0, 4, 0, 0, 1 }, 
     { 1, 0, 3, 0, 3, 0, 1 }, 
     { 1, 2, 0, 0, 0, 2, 1 }, 
     { 1, 1, 1, 1, 1, 1, 1 }, 
}; 

我需要得到这个2D阵列成2D ArrayList中,所以我可以通过增加行和列,以左右移动图案操纵它。例如,当我的方法要求的2行2列的转变我就能模式转移到这样的事情:

 { 0, 0, 0, 0, 0, 0, 0, 0, 0 } 
     { 0, 0, 0, 0, 0, 0, 0, 0, 0 } 
     { 0, 0, 1, 1, 1, 1, 1, 1, 1 }, 
     { 0, 0, 1, 2, 0, 0, 0, 2, 1 }, 
     { 0, 0, 1, 0, 3, 0, 3, 0, 1 }, 
     { 0, 0, 1, 0, 0, 4, 0, 0, 1 }, 
     { 0, 0, 1, 0, 3, 0, 3, 0, 1 }, 
     { 0, 0, 1, 2, 0, 0, 0, 2, 1 }, 
     { 0, 0, 1, 1, 1, 1, 1, 1, 1 }, 

我只是着眼于二维数组进入一个二维的ArrayList任何帮助将不胜感激!

+0

请参阅接受的答案中的评论,它是如何将阵列按照您想要的位置移动的? – SomeDude

回答

5

案例1它是短暂的,但需要隐蔽的基本类型根据需要引用类型(intIntegerArrays.asList();

Integer[][] pattern = new Integer[][]{ 
     { 1, 1, 1, 1, 1, 1, 1 }, 
     { 1, 2, 0, 0, 0, 2, 1 }, 
     { 1, 0, 3, 0, 3, 0, 1 }, 
     { 1, 0, 0, 4, 0, 0, 1 }, 
     { 1, 0, 3, 0, 3, 0, 1 }, 
     { 1, 2, 0, 0, 0, 2, 1 }, 
     { 1, 1, 1, 1, 1, 1, 1 }, 
}; 
List<List<Integer>> lists = new ArrayList<>(); 
for (Integer[] ints : pattern) { 
    lists.add(Arrays.asList(ints)); 
} 

案例2如果你不想隐蔽原始类型的引用类型:(int[][] pattern = new int[][]Integer[][] pattern = new Integer[][]

List<List<Integer>> lists = new ArrayList<>(); 
for (int[] ints : pattern) { 
    List<Integer> list = new ArrayList<>(); 
    for (int i : ints) { 
     list.add(i); 
    } 
    lists.add(list); 
} 
+0

在情况2中,当'list.add()'方法时,原始int也被隐式转换为整数。 –

+0

回答更新,我的意思是数组类型,我知道你在说什么,如果你没有将原始类型转换为引用类型'Arrays.asList()',则情况1不起作用。 –

+0

谢谢!案例1为我工作得很好:) – HexxNine

0

你可以做点事荷兰国际集团这样的:

public static List<Integer[]> twoDArrayList(int shift, int[][] input) 
{ 

    List<Integer[]> output = new ArrayList<Integer[]>(); 
    if(input.length == 0) return null; 
    int columnlength = input.length; 
    int rowlength = input[0].length; 
    if (columnlength != rowlength) return null; 

    int padsize = shift; 
    for(int i = 0; i < padsize; i++) 
    { 
     Integer[] zeroes = new Integer[shift+columnlength]; 

     for(int j = 0; j < shift+columnlength; j++) 
     { 
      zeroes[j] = 0; 
     } 
     output.add(zeroes); 
    } 

    for(int i = 0; i < columnlength; i++) 
    { 
     int[] row = input[i]; 
     int[] zeroes = new int[shift]; 
     List<Integer> temp = new ArrayList<Integer>(); 
     for(int j = 0; j < shift; j++) 
     { 
      temp.add(0); 
     } 
     for(int k = 0; k < row.length; k++) 
     { 
      temp.add(row[k]); 
     } 
     output.add(temp.toArray(new Integer[]{})); 
    } 

    return output; 
} 

观看演示here

当您提供转变为2:输出如下:

Running Shifting array... 
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 
Array no. 2 in the list is : 0 0 1 1 1 1 1 1 1 
Array no. 3 in the list is : 0 0 1 2 0 0 0 2 1 
Array no. 4 in the list is : 0 0 1 0 3 0 3 0 1 
Array no. 5 in the list is : 0 0 1 0 0 4 0 0 1 
Array no. 6 in the list is : 0 0 1 0 3 0 3 0 1 
Array no. 7 in the list is : 0 0 1 2 0 0 0 2 1 
Array no. 8 in the list is : 0 0 1 1 1 1 1 1 1 

当您提供3,你的输出是这样的:

Running Shifting array... 
Array no. 0 in the list is : 0 0 0 0 0 0 0 0 0 0 
Array no. 1 in the list is : 0 0 0 0 0 0 0 0 0 0 
Array no. 2 in the list is : 0 0 0 0 0 0 0 0 0 0 
Array no. 3 in the list is : 0 0 0 1 1 1 1 1 1 1 
Array no. 4 in the list is : 0 0 0 1 2 0 0 0 2 1 
Array no. 5 in the list is : 0 0 0 1 0 3 0 3 0 1 
Array no. 6 in the list is : 0 0 0 1 0 0 4 0 0 1 
Array no. 7 in the list is : 0 0 0 1 0 3 0 3 0 1 
Array no. 8 in the list is : 0 0 0 1 2 0 0 0 2 1 
Array no. 9 in the list is : 0 0 0 1 1 1 1 1 1 1