[英]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. 首先,我将数据存储到2D数组中,并且代码运行正常。 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. 然后,我使用nested for loops
从上述arrays
更改数据。 However, this is just the 1st array
from my 8 array
. 但是,这只是我的8 array
的1st array
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
? 还有其他方法可以减少这么多重复的代码,而不是复制如下所示的相同代码,然后将第1st array
转换为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. 之后,我想从方法中获取数据,结果全部为0,因为我非常确定结果将为1或0。但是由于显示的代码,我不知道如何将2D数组中的数据写入Array中。下面肯定是错误的。
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];
}
}
Well, I find myself a solution where I use the arrayList
to store the data. 好吧,我发现自己是一个使用arrayList
存储数据的解决方案。 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. 希望它可以帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.