简体   繁体   中英

get 8x8 array from 512x512 array and process it

I am new with Java I still developing an image watermarking program using DCT and SVD in Android, my program get 8x8 array from main array, do DCT and SVD in 8x8 array and return it to main array for simulation purpose, I create a 4x4 array, and trying to get 2x2 array from it. This is my code:

double[][] mainArray = new double[4][4];
double[][] subArray = new double[2][2];
double value = 1;
for (int i = 0; i < mainArray.length; i++){
   for(int j = 0; j <mainArray[0].length; j++){
    mainArray[i][j] = value;
    value++;
   }
}

//get 2x2 array from 4x4 array
int row = 0; int column = 0;
for (int m = 0; m < mainArray.length / 2; m++){
    for(int n = 0; n < mainArray[0].length / 2; n++){
        subArray[0][0] = mainArray[column][row]; subArray[0][1] = mainArray[column][row+1];
        subArray[1][0] = mainArray[column+1][row]; subArray[1][1] = mainArray[column+1][row+1];
        row += 2;
        column+2;
        System.out.print(subArray[0][0] + " " + subArray[0][1] + " " + subArray[1][0] + " " + subArray[1][1]);
    }
}

the result I want are:

1st iterate[1 2]  2nd iterate[3 4]
           [5 6]             [7 8]

3rd iterate[ 9 10] 4th iterate[11 12]
           [13 14]            [15 16]

but I got outofBoundException as a result Please help me, what's wrong in my code

You are close, but are missing a few things. The index out of bounds error is because of the fact that column is never reset to zero, it is bound to go out of bounds no matter what, because it never resets. The row variable should also be set in the outer loop not the inner with your code because it would update way too often the way you have it now. You also have column and row confused I believe. It is mainArray[row][column].

Also you can use your counters for row and column with m being the row and n being the column and increment both by two in this example, this way you don't have to create unnecessary variables. Keep in mind with this approach you would have to change the condition to remove the / 2 in the condition statement.

for (int row = 0; row < (mainArray.length); row += 2) {
        for (int column = 0; column < mainArray[0].length; column+=2) {
            subArray[0][0] = mainArray[row][column];
            subArray[0][1] = mainArray[row][column + 1];
            subArray[1][0] = mainArray[row + 1][column];
            subArray[1][1] = mainArray[row + 1][column + 1];
            //column += 2;
            System.out.print(subArray[0][0] + " " + subArray[0][1] + "\n"
                    + subArray[1][0] + " " + subArray[1][1] + "\n");
        }
    }

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