[英]Java, How to split array multidimensional by row and column
i have problem how to split my array to make zone 我在如何拆分数组以创建区域时遇到问题
my array is : 我的数组是:
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
if i set row=3 and column 3, result must: 如果我设置row = 3和column 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
my problem is, only get 我的问题是,只有得到
1 2 3 7 8 9 13 14 15
this is my source code 这是我的源代码
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));
}
}
Thank you Advance 谢谢你提前
the case had been solve.. 案子已经解决了
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]]
*/
thank you all 谢谢你们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.