简体   繁体   中英

Java loop through array of array

Suppose I have an array of an array:

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

and i want to loop through img in a 2x2 block... example:

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

then you start by looking at the first 2x2 sub-array (from the top-left)

2,  4
3,  3

We then look at the next 2x2 block

31,  31
21,  41

other blocks would be 1,2,3,2 and 10,20,20,30...

How do make a loop so it goes through like this? Essentially I am doing this so I can find the average of the values in the block, and replace each element in the array by that average.

You will need two nested for loops. But unlike normal for loops, instead of incrementing your looping variables, in both cases, add 2 to your index. Then, inside the inner for loop, assuming you have looping indexes i and j , refer to your 4 values with img[i][j] , img[i + 1][j] , img[i][j + 1] , and img[i + 1][j + 1] . But you'll have to be careful if row or col is odd.

You could use a structure like this:

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

May be this help you:

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");

put tile size if your need other size then 2*2:

EDIT
Now I am providing complete code.

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");
           }
    }
}

This is actually working:

A running instance

 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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM