2015-03-25 64 views
1

分裂陣列多維我有問題如何分割我的陣列,使區Java中,如何通過行和列

我的數組是:

   1, 2, 3, 4, 5, 6 
      7, 8, 9, 10, 11, 12 
      13, 14, 15, 16, 17, 18 
      19, 20, 21, 22, 23, 24 
      25, 26, 27, 28, 29, 30 
      31, 32, 33, 34, 35, 36 

如果我設置行= 3和第3列,結果必然:

 
1 2 3 
7 8 9 
13 14 15 
 
4 5 6 
10 11 12 
16 17 18 
 
19 20 21 
25 26 27 
31 32 33 
 
22 23 24 
28 29 30 
34 35 36 

我的問題是,只得到

 
1 2 3 
7 8 9 
13 14 15 

,這是我的源代碼

int dataArray[][] = new int[][]{ 
     {1, 2, 3, 4, 5, 6}, 
     {7, 8, 9, 10, 11, 12}, 
     {13, 14, 15, 16, 17, 18}, 
     {19, 20, 21, 22, 23, 24}, 
     {25, 26, 27, 28, 29, 30}, 
     {31, 32, 33, 34, 35, 36} 
    }; 


    int row = 3; 
    int column = 3; 

    int zone = 4; 
    int copyArray[][] = new int[row][column]; 


    int countJ = 0, countK = 0; 

    for (int j = 0; j < row; j++) { 
     for (int k = 0; k < column; k++) { 
      copyArray[countJ][countK] = dataArray[j][k]; 
      copyArray[countK][countJ] = dataArray[k][j]; 
      countK++; 
     } 
     countK = 0; 
     countJ++; 
    } 

    for (int i = 0; i < zone; i++) { 
     System.out.println(Arrays.deepToString(copyArray)); 
    } 

} 

謝謝推進

+0

你說什麼是實際結果,但預期是什麼? – 2015-03-25 07:49:09

+0

您的源代碼是否提供了正確的結果?如果我給出錯誤的結果是什麼問題。請解釋你的問題。 – Razib 2015-03-25 07:49:43

+0

我不知道如何得到所有結果,只有1個區域我可以..另一個區域仍然失敗 – 2015-03-25 07:51:41

回答

0

的情況下已經解決..

int dataArray[][] = new int[][]{ 
     {1, 2, 3, 4, 5, 6}, 
     {7, 8, 9, 10, 11, 12}, 
     {13, 14, 15, 16, 17, 18}, 
     {19, 20, 21, 22, 23, 24}, 
     {25, 26, 27, 28, 29, 30}, 
     {31, 32, 33, 34, 35, 36}}; 

    int baris = 3; 
    int kolom = 3; 
    int lebarZona = 2; 
    int tinggiZona = 2; 
    int counterTinggi = 0; 
    ArrayList<int[][]> daftarZona = new ArrayList<int[][]>(); 
    counterTinggi = 0; 
    int counterBaris = 0; 
    int counterKolom = 0; 

    for (int i = 0; i < tinggiZona; i++) { 
     for (int j = 0; j < lebarZona; j++) { 
      int copyArray[][] = new int[baris][kolom]; 

      for (int k = 0; k < baris; k++) { 
       System.arraycopy(dataArray[counterBaris], counterKolom, copyArray[k], 0, kolom); 
       counterBaris++; 
      } 

      daftarZona.add(copyArray); 
     } 
     counterBaris = 0; 
     counterKolom += kolom; 
    } 

    for (int i = 0; i < daftarZona.size(); i++) { 
     System.out.println(Arrays.deepToString(daftarZona.get(i))); 
    } 



    /* 
    * Result : 
    * [[1, 2, 3], [7, 8, 9], [13, 14, 15]] 
    *[[19, 20, 21], [25, 26, 27], [31, 32, 33]] 
    *[[4, 5, 6], [10, 11, 12], [16, 17, 18]] 
    *[[22, 23, 24], [28, 29, 30], [34, 35, 36]] 
    */ 

謝謝你們全都