将二维数组转换为二维数组的多个块
问题描述:
我试图将一个16×4000维数组转换为1000个8×8数组块。我应该在运行此代码后以1000个这样的块结束。这是我写的,我需要知道我做错了什么,因为这段代码只给了我500块的东西。将二维数组转换为二维数组的多个块
blockedCode(d); //d is a two D 16 x 4000 array
private static void blockedCode(int[][] array)
{
int one=0;
for (int i = 0; i < array.length; i += 8)
{
for (int j = 0; j < array[i].length; j += 8)
{
int block = (((i/8) * 8) + (j/8));
System.out.println("Block : " + block);
int[][] newArray = new int[8][8];
int newRow = 0;
List list = new ArrayList<>();
for (int k = i; k < (i + 8); k++)
{
int newColumn = 0;
for (int l = j; l < (j + 8); l++)
{
// This is where you are getting your array inside the given block.
newArray[newRow][newColumn] = array[k][l];
one++;
// System.out.format("[%-1s][%-1s] : %-3s ", newRow, newColumn, newArray[newRow][newColumn++]);
}
newRow++;
}
}
}
}
答
问题是与你的逻辑计算块编号:
int block = (((i/8) * 8) + (j/8));
应改为:
int block = (((i/8) * (array[i].length/8)) + (j/8));
你也需要增加内内newColumn变量for loop ..
for (int k = i; k < (i + 8); k++)
{
int newColumn = 0;
for (int l = j; l < (j + 8); l++)
{
// This is where you are getting your array inside the given block.
newArray[newRow][newColumn] = array[k][l];
one++;
newColumn++; //add this line
// System.out.format("[%-1s][%-1s] : %-3s ", newRow, newColumn, newArray[newRow][newColumn++]);
}
newRow++;
}
你我thod看起来充满了未经测试的代码,评论和空行...我建议你用TDD从头开始编写程序,然后你就会知道它的工作原理。 – 2014-11-22 08:37:58
我喜欢'int one = 0' :) – 2014-11-22 08:40:44