我需要将 double 类型的 1D 数组(直方图)保存在 2D 数组(表示图片的一部分)中。
实际上我已经这样做了(简化的代码):
private ArrayList<double[]>[][] calculateSignature(BufferedImage screenshot, int blocksizeX, int blocksizeY)
{
ArrayList<double[]>[][] signature = new ArrayList[2][2];
//!!!!! Type safety: The expression of type ArrayList[][] needs unchecked conversion to conform to ArrayList<double[]>[][] !!!!!
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
signature[x][y] = calculateHistogram(screenshot.getSubimage(x * blocksizeX, y * blocksizeY, blocksizeX, blocksizeY));
}
}
return signature;
}
private ArrayList<double[]> calculateHistogram(BufferedImage screenshot)
{
double[] histogramRGB = new double[24];
...
ArrayList<double[]> imageLUT = new ArrayList<double[]>();
imageLUT.add(histogramRGB);
return imageLUT;
}
这可行,但我认为这不是一个好的实现。对此有什么改进吗?此外,我在上面的代码注释中收到这样的错误。
最佳答案
如果您只添加单个元素(即一维直方图),为什么要使用ArrayList
。如果您只需要二维数组中的一维数组,正如您在问题中所说,则不需要 ArrayList,您可以将代码简化为:
private double[][][] calculateSignature(BufferedImage screenshot, int blocksizeX, int blocksizeY)
{
double[][][] signature = new double[2][2][24];
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
signature[x][y] = calculateHistogram(screenshot.getSubimage(x * blocksizeX, y * blocksizeY, blocksizeX, blocksizeY));
}
}
return signature;
}
private double[] calculateHistogram(BufferedImage screenshot)
{
double[] histogramRGB = new double[24];
...
return histogramRGB;
}
关于java - 将一维数组保存到另一个二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25448434/