简体   繁体   中英

Store data from 2D array to an Array

First of all, I store the data into a 2D array and the code is working fine. The code is shown as below,

  for(int a = 0; a < bitmap1.getWidth(); a++){
      // ------ This is X
                        for(int b = 0; b < bitmap1.getHeight()-1; b++){
                            // ---------- This is Y
                            intArray1[a][b] = Integer.toHexString(bitmap1.getPixel(a,b));
                            intArray2[a][b] = Integer.toHexString(bitmap2.getPixel(a,b));
                            intArray3[a][b] = Integer.toHexString(bitmap3.getPixel(a,b));
                            intArray4[a][b] = Integer.toHexString(bitmap4.getPixel(a,b));
                            intArray5[a][b] = Integer.toHexString(bitmap5.getPixel(a,b));
                            intArray6[a][b] = Integer.toHexString(bitmap6.getPixel(a,b));
                            intArray7[a][b] = Integer.toHexString(bitmap7.getPixel(a,b));
                            intArray8[a][b] = Integer.toHexString(bitmap8.getPixel(a,b));
                        }
                    }//end of Nested FOR

Then i use nested for loops to change the data from the arrays as above. However, this is just the 1st array from my 8 array . Is there any others way to reduce so much duplicated code instead of copying the same code as shown in below and do the convert from the 1st array to 8 array ?

    int [][] arrayOneZero = new int [array1.length][array1.length];
    for(int a = 0; a < array1.length; a++){
        for(int b = 0; b < array1.length-1; b++){
            if(array1[a][b].equals("ffffffff")){
                //This is White
                arrayOneZero[a][b] = 1;
            }else if(array1[a][b].equals("ff000000")){
                //This is Black
                arrayOneZero[a][b] = 0;
            }else if(array1[a][b].equals("00000000")){
                //THis is Black
                arrayOneZero[a][b] = 0;
            }
        }

After that, I want to get the data from the method and the result is all 0, since i pretty sure the result will be 1 or 0. But I dont know how to write the data from 2D array to an Array since the code shown below is definitely wrong.

 int [][] arrayReturned1 = getArrayText(intArray1);
 int [] colorValue = new int[bitmap1.getWidth()*bitmap1.getHeight()];
      for(int a = 0; a < bitmap1.getWidth(); a++) {
            for(int b = 0; b < bitmap1.getHeight()-1; b++) {
                   arrayReturned1[a][b] = colorValue[a];
             }
      }

Question is edited, but the issue is still remain.

Well, I find myself a solution where I use the arrayList to store the data. And it works fine.

 ArrayList<String>arrayList = new ArrayList<>();
                    for(int a = 0; a < bitmap1.getWidth(); a++){
                        for(int b = 0; b < bitmap1.getHeight(); b++){
                            String a1 = String.valueOf(arrayInput1[a][b]);
                            String a2 = String.valueOf(arrayInput2[a][b]);
                            String a3 = String.valueOf(arrayInput3[a][b]);
                            String a4 = String.valueOf(arrayInput4[a][b]);
                            String a5 = String.valueOf(arrayInput5[a][b]);
                            String a6 = String.valueOf(arrayInput6[a][b]);
                            String a7 = String.valueOf(arrayInput7[a][b]);
                            String a8 = String.valueOf(arrayInput8[a][b]);
                            arrayList.add(a1+a2+a3+a4+a5+a6+a7+a8); // Store 1110001 into ArrayList
                        }
                    }

Hope it could help someone else.

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