Mag*_*nus 7 java arrays sorting
我对Java很新,但进入它.但是,我无法理解为什么我在这里找到的这个例子不起作用:
上课时:
String[][] spritesPaint = new String[20][20];
Run Code Online (Sandbox Code Playgroud)
在方法中:
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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想要做的是有一个二维数组,我在屏幕上存储对象的Y位置和与该对象相关的图像路径,然后按Y位置整数对其进行排序.这应该允许我以正确的顺序将元素绘制到屏幕上以获得等角透视图.
但是,我不断收到此错误:
The method sort(T[], Comparator<? super T>) in the type Arrays
is not applicable for the arguments (Integer, new Comparator<Integer[]>(){})
Run Code Online (Sandbox Code Playgroud)
请帮助我,我一直在扭曲我的大脑数小时试图理解为什么我现在得到这个错误.
该错误消息意味着,您传递的不是 T 类型(通用)的数组,而是 Integer。那么,你有:
Arrays.sort(Integer.valueOf(spritesPaint[0].toString()), ...
Run Code Online (Sandbox Code Playgroud)
你想通过
Arrays.sort(arrayToBeSorted, ...
Run Code Online (Sandbox Code Playgroud)
(另外,你的变量可以使用更好的名称,因为我真的不明白这个例子在做什么......)
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |