猿问

将数据从二维数组存储到数组

首先,我将数据存储到二维数组中,代码运行良好。代码如下所示,


  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

然后我nested for loops用来改变arrays上面的数据。但是,这只是1st array来自我的8 array. 有没有其他方法可以减少如此多的重复代码,而不是复制如下所示的相同代码并从1st arrayto转换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;

            }

问题已编辑,但问题仍然存在。


猛跑小猪
浏览 114回答 1
1回答

湖上湖

好吧,我为自己找到了一个使用arrayList来存储数据的解决方案。它工作正常。&nbsp;ArrayList<String>arrayList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int a = 0; a < bitmap1.getWidth(); a++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int b = 0; b < bitmap1.getHeight(); b++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a1 = String.valueOf(arrayInput1[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a2 = String.valueOf(arrayInput2[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a3 = String.valueOf(arrayInput3[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a4 = String.valueOf(arrayInput4[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a5 = String.valueOf(arrayInput5[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a6 = String.valueOf(arrayInput6[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a7 = String.valueOf(arrayInput7[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String a8 = String.valueOf(arrayInput8[a][b]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrayList.add(a1+a2+a3+a4+a5+a6+a7+a8); // Store 1110001 into ArrayList&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }希望它可以帮助别人。
随时随地看视频慕课网APP

相关分类

Java
我要回答