[英]Java, two dimensional array sort
I'm quite new to Java, but getting into it. 我对Java很新,但进入它。 However, I can't wrap my head around why this example, which I found here, does not work:
但是,我无法理解为什么我在这里找到的这个例子不起作用:
At start of class: 上课时:
String[][] spritesPaint = new String[20][20];
In method: 在方法中:
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);
}
}
}
}
Basically, what I want to do is have a two dimensional array where I store Y position of object on the screen and an image path also related to that object, then sort it by the Y position integer. 基本上,我想要做的是有一个二维数组,我在屏幕上存储对象的Y位置和与该对象相关的图像路径,然后按Y位置整数对其进行排序。 This should allow me to paint elements to the screen in the correct order for an isometric perspective.
这应该允许我以正确的顺序将元素绘制到屏幕上以获得等角透视图。
However, I keep getting this error: 但是,我不断收到此错误:
The method sort(T[], Comparator<? super T>) in the type Arrays
is not applicable for the arguments (Integer, new Comparator<Integer[]>(){})
Please help me, I've been twisting my brain for hours trying to understand why I get this error now. 请帮助我,我一直在扭曲我的大脑数小时试图理解为什么我现在得到这个错误。
Integer.valueOf(spritesPaint[0].toString())
is a single integer, however from your description it seems that you want to sort the strings in the 1-dimensional array spritesPaint[0]
as if they were integers. Integer.valueOf(spritesPaint[0].toString())
是一个整数,但是根据你的描述,你似乎想要对一维数组spritesPaint[0]
的字符串进行排序,就好像它们是整数一样。 Here is a modification that will do just that: 这是一个修改,将做到这一点:
Arrays.sort(spritesPaint[0], new Comparator<String>() {
@Override public int compare(final String entry1, final String entry2) {
final Integer time1 = Integer.valueOf(entry1);
final Integer time2 = Integer.valueOf(entry2);
return time1.compareTo(time2);
}
});
Alternatively, if you are trying to sort the first dimension of the array by the first element of the second dimension of the array, modify as follows: 或者,如果您尝试按阵列的第二个维度的第一个元素对数组的第一个维度进行排序,请按如下所示进行修改:
Arrays.sort(spritesPaint, new Comparator<String[]>() {
@Override public int compare(final String[] entry1, final String[] entry2) {
final Integer time1 = Integer.valueOf(entry1[0]);
final Integer time2 = Integer.valueOf(entry2[0]);
return time1.compareTo(time2);
}
});
The error message means that, instead of passing an Array of type T (generic), you are passing an Integer. 错误消息意味着,您传递的是Integer,而不是传递类型为T(通用)的数组。 So, where you have:
所以,你有:
Arrays.sort(Integer.valueOf(spritesPaint[0].toString()), ...
you want to pass in 你想传入
Arrays.sort(arrayToBeSorted, ...
(Also, your variables could do with better names because I really don't understand what this example is doing...) (另外,你的变量可以用更好的名字,因为我真的不明白这个例子在做什么...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.