Java 检索到的值具有空值。如何消除它们?
我已经成功地从CompareExargb方法中检索到RGB像素值。而且输出似乎不令人信服,因为输出也包含一个空字符。我怎样才能消除它们 其次,当我尝试组合字符串[][]char 1+char 2时,我得到一个错误,指出它是“+”类型的错误操作数。但是我想合并2个字符串,以便能够将其转换为ASCII字符 下面是compareHexaRGB方法的代码:Java 检索到的值具有空值。如何消除它们?,java,arrays,null,Java,Arrays,Null,我已经成功地从CompareExargb方法中检索到RGB像素值。而且输出似乎不令人信服,因为输出也包含一个空字符。我怎样才能消除它们 其次,当我尝试组合字符串[][]char 1+char 2时,我得到一个错误,指出它是“+”类型的错误操作数。但是我想合并2个字符串,以便能够将其转换为ASCII字符 下面是compareHexaRGB方法的代码: public class compareHexaRGB { private static int w; private static int h
public class compareHexaRGB
{
private static int w;
private static int h;
private static BufferedImage img;
private static BufferedImage img2;
private static String[][] check_hex2;
private static String[][] check_hex4;
private static String[][] message;
public static void compareHexaRGB(BufferedImage image, BufferedImage image2, int width, int height) throws IOException
{
w = width;
h = height;
img = image;
img2 = image2;
}
public void check() throws IOException
{
getPixelRGB1 pixel = new getPixelRGB1();
getPixelData1 newPD = new getPixelData1();
int[] rgb;
int count = 0;
int[][] pixelData = new int[w * h][3];
check_hex2 = new String[w][h];
check_hex4 = new String[w][h];
for(int i = 0; i < w; i++)
{
for(int j = 0; j < h; j++)
{
rgb = newPD.getPixelData(img, i, j);
for(int k = 0; k < rgb.length; k++)
{
pixelData[count][k] = rgb[k];
}
if(pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))
{
System.out.println("\nPixel values at position 2 are the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j]);
}
if(pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))
{
System.out.println("\nPixel values at position 4 are the same." + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]);
}
if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]))
{
System.out.println("\nPixel values at position 2 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j]);
check_hex2[i][j] = pixel.display_img2Hex2()[i][j];
System.out.println("\nOutput Hex 2: " + check_hex2[i][j]);
}
if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]))
{
System.out.println("\nPixel values at position 4 are not the same." + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]);
check_hex4[i][j] = pixel.display_img2Hex4()[i][j];
System.out.println("\nOutput Hex 4: " + check_hex4[i][j]);
}
if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])))
{
System.out.println("\nOne of the pixel values at position 2 and 4 are not the same." + "\n" + pixel.display_imgHex2()[i][j] + " " + pixel.display_img2Hex2()[i][j] + "\n" + pixel.display_imgHex4()[i][j] + " " + pixel.display_img2Hex4()[i][j]);
if(!pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j]) || (pixel.display_imgHex2()[i][j].equals(pixel.display_img2Hex2()[i][j])))
{
check_hex2[i][j] = pixel.display_img2Hex2()[i][j];
System.out.println("\nOutput Hex 2: " + check_hex2[i][j]);
}
if(!pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j]) || (pixel.display_imgHex4()[i][j].equals(pixel.display_img2Hex4()[i][j])))
{
check_hex4[i][j] = pixel.display_img2Hex4()[i][j];
System.out.println("\nOutput Hex 4: " + check_hex4[i][j]);
}
}
count++;
System.out.println("\nOutput Count: " + count);
}
}
}
public String[][] getCheck_hex2()
{
return check_hex2;
}
public String[][] getCheck_hex4()
{
return check_hex4;
}
}
如有任何建议或更正,我将不胜感激 - 由于这些位置的像素是相同的,所以会得到很多空值。在
方法中,当像素相同时,不向该位置的数组中添加任何内容,因此它将保持为空。在这些点上添加一个放置字符,如check()
或其他-
- 您的打印方法是在一行上打印每个元素,如果您像打印矩阵一样打印二维数组,则可读性会更好。您可以通过将方法修改为
for (int i = 0; i < inn.length; i++) { for(int j = 0; j < inn[i].length; j++) { System.out.print(inn[i][j] + " "); } System.out.println(); }
- 当您尝试添加数组时会出现错误,因为您根本无法做到这一点-在不循环内容的情况下将两个数组添加到一起。您将需要一个与上面类似的循环
char1 = hexRGB.getCheck_hex2(); char2 = hexRGB.getCheck_hex4(); //assuming char1 and char2 are the same size and symmetrical String[][] combine = new String[char1.length][char1[0].length] for (int i = 0; i < char1.length; i++) { for(int j = 0; j < char1[i].length; j++) { // This will only concatenate the strings though, // If you want the numerical addition you will have to // convert them to ints first combine[i][j] = char1[i][j] + char2[i][j]; } }
char1=hexRGB.getCheck_hex2(); char2=hexRGB.getCheck_hex4(); //假设char1和char2大小相同且对称 字符串[][]组合=新字符串[char1.length][char1[0].length] for(int i=0;i
for (int i = 0; i < inn.length; i++)
{
for(int j = 0; j < inn[i].length; j++)
{
System.out.print(inn[i][j] + " ");
}
System.out.println();
}
for(String[] arr : inn)
System.out.println(Arrays.toString(arr));
char1 = hexRGB.getCheck_hex2();
char2 = hexRGB.getCheck_hex4();
//assuming char1 and char2 are the same size and symmetrical
String[][] combine = new String[char1.length][char1[0].length]
for (int i = 0; i < char1.length; i++)
{
for(int j = 0; j < char1[i].length; j++)
{
// This will only concatenate the strings though,
// If you want the numerical addition you will have to
// convert them to ints first
combine[i][j] = char1[i][j] + char2[i][j];
}
}