Java循环遍历数组

时间:2013-03-01 20:22:06

标签: java arrays multidimensional-array

假设我有一个数组数组:

double[][] img = new double[row][col];

我希望在2x2块中循环img ...例如:

2,  4, 31, 31   
3,  3, 21, 41
1,  2, 10, 20
3,  2, 20, 30

然后你开始看第一个2x2子阵列(从左上角开始)

2,  4
3,  3

然后我们看看下一个2x2块

31,  31
21,  41

其他街区将是1,2,3,2和10,20,20,30 ......

如何制作一个循环,让它像这样经过?基本上我这样做,所以我可以找到块中值的平均值,并用该平均值替换数组中的每个元素。

3 个答案:

答案 0 :(得分:3)

您需要两个嵌套的for循环。但与普通for循环不同,在两种情况下,不是增加循环变量,而是在索引中添加2。然后,在内部for循环内,假设您有循环索引ij,请使用img[i][j]img[i + 1][j]img[i][j + 1]和您的4个值来引用img[i + 1][j + 1]。但是,如果rowcol是奇数,则必须小心。

答案 1 :(得分:2)

你可以使用这样的结构:

double[][] img = new double[row][col];
//This will break if row or col are odd, make sure you are always passing an even amount or check for this case.
for (int i = 0; i < row; i+=2) {
    for (int j = 0; j < col; j+=2) {
        //Do what you need with these values:
        img[i][j];     //Top left
        img[i+1][j];   //Top right
        img[i][j+1];   //Bottom left
        img[i+1][j+1]; //Bottom right
    }
}

答案 2 :(得分:1)

可以帮助你:

tile = 2;
for(i = 0; i < row; i = tile + i)
 for(j = 0; j < col; j= tile + j)
  for(r = 0; r < tile; r++)   
    for(c = 0; c < tile; c++) 
      System.out.print(" " + img[i+r][j+c]);
    System.out.print("\n");

如果您需要其他尺寸,则为tile尺寸,然后为2 * 2:

修改
现在我提供完整的代码。

class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] img = { 
                    {55, 60, 65, 1},
                    {95, 90, 85, 5},
                    {5,  0,  8,  5},  
                    {53, 60, 89, -5}
        };


        int tile=2; 
        int row=4; 
        int col=4;
        int i, r;
        int j, c;

        tile = 2;
        for(i = 0; i < row; i= tile + i)
           for(j = 0; j < col; j= tile + j){
              for(r = 0; r < tile; r++){   
                 for(c = 0; c < tile; c++) 
                    System.out.print(" ", img[i+r][j+c]);
                 System.out.println("");
              }
              System.out.println("\n");
           }



        int img2[][] = { 
                  // 1   2   3   4  5  6  7  8  9 
                    {55, 60, 65, 1, 2, 4, 1, 4, 0},
                    {95, 90, 85, 5, 3, 6, 5, 0, 8},
                    {5,  0,  8,  5,-1, 2, 2, 5, 6},  
                    {95, 90, 85, 5, 3, 6, 5, 0, 8},
                    {55, 60, 65, 1, 2, 4, 1, 4, 0},
                    {5,  0,  8,  5,-1, 2, 2, 5, 6},  
                    {1,  2,  3,  4, 5, 6, 7, 8, 9}, 
                    {1,  2,  3,  4, 5, 6, 7, 8, 9}, 
                    {1,  2,  3,  4, 5, 6, 7, 8, 9}
                  };

         row = 9; 
         col = 9;
         tile = 3;

         for(i = 0; i < row; i= tile + i)
           for(j = 0; j < col; j= tile + j){
              for(r = 0; r < tile; r++){   
                 for(c = 0; c < tile; c++) 
                    System.out.print(" ", img[i+r][j+c]);
                 System.out.println("");
              }
              System.out.println("\n");
           }
    }
}

这实际上有效:

正在运行的实例

 55  60 
 95  90 


 65   1 
 85   5 


  5   0 
 53  60 


  8   5 
 89  -5 


 55  60  65 
 95  90  85 
  5   0   8 


  1   2   4 
  5   3   6 
  5  -1   2 


  1   4   0 
  5   0   8 
  2   5   6 


 95  90  85 
 55  60  65 
  5   0   8 


  5   3   6 
  1   2   4 
  5  -1   2 


  5   0   8 
  1   4   0 
  2   5   6 


  1   2   3 
  1   2   3 
  1   2   3 


  4   5   6 
  4   5   6 
  4   5   6 


  7   8   9 
  7   8   9 
  7   8   9