2016-03-03 226 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