简体   繁体   中英

Populating 2D array by column java

I have 10 2D arrays which are 7x5, each represents an image storing an average of the RGB values. I now want to copy each of these arrays storing the 35 values into a 2D array of size 35x10 however I want to store each by column, ie. copying the first 2D array into the first column of the 35x10 array, so each column stores one of the images vertically.

So I am trying to store each of these 2D arrays in separate columns in the same 2D array:

Image 1:

0 34 40 50 0 
91 55 60 64 102 
89 65 68 78 86 
74 66 98 77 77 
107 65 68 74 83 
119 70 55 64 94 
0 52 59 48 0 

Image 2:

0 27 38 40 0 
67 80 85 97 94 
90 82 84 110 119 
99 66 117 116 115 
111 93 82 122 149 
119 106 102 133 123 
0 52 72 58 0 

Desired output:

0   0 
34  27
40  38
50  40
0   0 
91  67
55  80
60  85
64  97
102 94
....

This is the code I have so far:

public static void populateArray() {
    for (int r = 0; r < image1.length; r++) {
        for (int c = 0; c < image1[r].length; c++) {
            pixArray[r][c] = image1[r][c];
        }
    }

    System.out.println("\n");
    for (int r = 0; r < image2.length; r++){
      for(int c=0;c < image2[r].length;c++){
        pixArray[r][c+1]=image2[r][c];

      }
    }

    for (int i = 0; i < pixArray.length; i++) {
        for (int j = 0; j < pixArray[i].length; j++) {
            System.out.print(pixArray[i][j] + " ");
        }
        System.out.println();
    }

    }
}

You have to do something like this

 for (int r = 0; r < image1.length; r++) {
    for (int c = 0; c < image1[r].length; c++) {
        pixArray[r*image1[0].length+c][0] = image1[r][c];
    }
}

 for (int r = 0; r < image2.length; r++) {
    for (int c = 0; c < image2[r].length; c++) {
        pixArray[r*image2[0].length+c][1] = image2[r][c];
    }
}

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