我很新于Java编程,但正逐渐入门。然而,我无法理解为什么这个例子(来自此处)不起作用:
在类开始时:
String[][] spritesPaint = new String[20][20];
在方法中:
for (int funct1 = 0; funct1 <= 2; funct1++) {
if (funct1 == 0) {
for (int funct2 = 0; funct2 < rEnemyNumber; funct2++) {
spritesPaint[0][funct2] = new Integer(rEnemyY[funct2])
.toString();
spritesPaint[1][funct2] = rEnemyGraphic[funct2];
}
} else if (funct1 == 1) {
Arrays.sort(Integer.valueOf(spritesPaint[0].toString()),
new Comparator<Integer[]>() {
@Override
public int compare(final Integer[] entry1,
final Integer[] entry2) {
final Integer time1 = entry1[0];
final Integer time2 = entry2[0];
return time1.compareTo(time2);
}
});
} else if (funct1 == 2) {
for (int funct3 = 0; funct3 < rEnemyNumber; funct3++) {
if (rEnemyCheck[funct3] == true) {
nextPaint = getImage(base, rEnemyGraphic[funct3]);
System.out.println("Next: " + nextPaint);
g.drawImage(nextPaint, rEnemyX[funct3] + worldCenterX,
rEnemyY[funct3] + worldCenterY, this);
}
}
}
}
基本上,我想做的是有一个二维数组,在其中存储对象在屏幕上的Y轴位置和与该对象相关的图像路径,然后按Y轴位置整数排序。这应该允许我以正确的顺序为等距透视画面绘制元素。
但是,我一直收到这个错误:
The method sort(T[], Comparator<? super T>) in the type Arrays
is not applicable for the arguments (Integer, new Comparator<Integer[]>(){})
请帮助我,我已经苦思冥想数小时,试图理解为什么我现在会出现这个错误。
Arrays.sort()
的第一个参数必须是一个数组。你正在传递一个Integer
。 - jlordoArrays.sort()
只接受一个数组作为它的第一个参数。 - jlordo