简体   繁体   English

如何将2D数组的数量添加到数组

[英]how to add numbers of 2D array to array

For now, i am having 8 number of 2D array eg arrayInput1 until arrayInput8 . 现在,我有8个2D array例如arrayInput1直到arrayInput8 So now the data from the 2D array is 1 and 0 and let set the size of the array is 25. 因此,现在2D数组的数据为10并设置数组的大小为25。

 for(int a=0; a<5; a++){
     for(int b=0; b<4; b++){
         arrayInput1[a][b] //----> The result here is 0
         arrayInput2[a][b] //----> The result here is 1
         arrayInput3[a][b] //----> The result here is 0
         arrayInput4[a][b] //----> The result here is 1
         arrayInput5[a][b] //----> The result here is 0
         arrayInput6[a][b] //----> The result here is 1
         arrayInput7[a][b] //----> The result here is 0
         arrayInput8[a][b] //----> The result here is 1
     }
 }//end of nested for loops

So the result from those arraiInput i will get 01010101 . 因此,这些arraiInput i的结果将为01010101 Then I want to store them in an array . 然后我想将它们存储在array So the first index of array will print 01010101 所以array的第一个索引将打印01010101

So the question is How to do that ? 所以问题是如何做到的?

You've got pixel data in eight bit-planes. 您已经在八个位平面中获得了像素数据。 You want to merge these into a single plane of eight-bit integer pixels. 您想将它们合并到一个八位整数像素的平面中。 Here's an example of how to do it: 这是一个如何做的例子:

int newPixels[][] = 
    new int[arrayInput1.length][arrayInput1[0].length];

for(int a=0; a<arrayInput.length; a++){
    for(int b=0; b<arrayInput1[0].length; b++){
         newPixels[a][b] =
         arrayInput1[a][b] * 128 +
         arrayInput2[a][b] * 64 +
         arrayInput3[a][b] * 32 +
         arrayInput4[a][b] * 16 +
         arrayInput5[a][b] * 8 +
         arrayInput6[a][b] * 4 +
         arrayInput7[a][b] * 2 +
         arrayInput8[a][b];
    }
}

Here I've assumed that arrayInput1 holds the bits that should become the most significant bits in the merged pixels, arrayInput2 holds the next highest significant bits, and so on down to arrayInput8 which holds the least significant bits. 在这里,我假设arrayInput1保留应成为合并像素中最高有效位的位, arrayInput2保留下一个最高有效位,依此类推,直到arrayInput8保留最低有效位。

You should initialize it directly : int[] data = {0,1,0,1,0,1,0,1}; 您应该直接对其进行初始化: int[] data = {0,1,0,1,0,1,0,1}; and int[][] data2d = {data, data, data, data, data, data, data, data} int[][] data2d = {data, data, data, data, data, data, data, data}

Use a StringBuilder (or similar) to build a string from the int values and add it to a String array. 使用StringBuilder(或类似版本)从int值构建字符串并将其添加到String数组。 I was unsure about the array sizes so I used two constants instead. 我不确定数组的大小,所以我改用了两个常量。

String[][] result = new String[ROW_SIZE][COLUMN_SIZE];
for(int a = 0; a < ROW_SIZE; a++){
   for(int b = 0; b < COLUMN_SIZE; b++){
      StringBuilder builder = new StringBuilder(8);        
      builder.append(arrayInput1[a][b]);
      builder.append(arrayInput1[a][b]); 
      builder.append(arrayInput1[a][b]);
      builder.append(arrayInput2[a][b]);
      builder.append(arrayInput3[a][b]);
      builder.append(arrayInput4[a][b]);
      builder.append(arrayInput5[a][b]);
      builder.append(arrayInput6[a][b]);
      builder.append(arrayInput7[a][b]);
      builder.append(arrayInput8[a][b]);
      result[a][b] = builder.toString();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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