我需要在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;
}
这有效,但我认为这不是一个好的实现。那有什么改进吗?此外,我在上面的代码注释中收到了类似的错误。
答案 0 :(得分:1)
如果只添加一个元素,即为1D直方图,为什么要使用ArrayList
?如果您在问题中只需要2D阵列中的一维数组,则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;
}