将2D数组转换为多个2D数组块

时间:2014-11-22 08:13:28

标签: java arrays

我正在尝试将16 x 4000维数组转换为1000个8 x 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++;
                }
        }   
    }
}

1 个答案:

答案 0 :(得分:0)

问题在于您计算块编号的逻辑:

int block = (((i / 8) * 8) + (j / 8));

应该改为:

int block = (((i/8) * (array[i].length/8)) + (j / 8));

此外,您必须在内部for循环内增加newColumn变量。

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++;
}