Java compareTo 数组排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27762343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java compareTo array sort
提问by JVTura
I have two classes Main
and Object
. I need to sort the objects in array in ascending order according to its value. I return -1, 1, and 0 from compareTo and I need to run a for loop accordingly to sort my array. I don't want to use Arrays.sort, I need to do it manually. The sorting part n the Main
class does not work. Any help could be useful. Thank you.
我有两个班级Main
和Object
. 我需要根据其值按升序对数组中的对象进行排序。我从 compareTo 返回 -1、1 和 0,我需要相应地运行 for 循环来对我的数组进行排序。我不想使用 Arrays.sort,我需要手动进行。Main
课程中的排序部分不起作用。任何帮助都可能有用。谢谢你。
public class Main {
public static void main(String[] args) {
Object[] arr = new Object[6];
arr[0] = new Object(2);
arr[1] = new Object(5);
arr[2] = new Object(3);
arr[3] = new Object(1);
arr[4] = new Object(6);
arr[5] = new Object(4);
System.out.println("List of instances");
for (int i = 0; i < 5; i++) {
System.out.println(arr[i].getValue());
}
System.out.println();
Object tempVar;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < 5; j++) {
int result = arr[i].compareTo(arr[i]);
if (result == -1) {
tempVar = arr[j + 1];
arr[j + 1] = arr[i];
arr[i] = tempVar;
}
}
}
System.out.println("List of sorted instances");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].getValue());
}
}
}
}
public class Object implements Comparable<Object> {
private int value;
public Object(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
@Override
public int compareTo(Object o) {
int result = 0;
if (this.value > o.getValue()) {
result = 1;
} else if (this.value < o.getValue()) {
result = -1;
} else if (this.value == o.getValue()) {
result = 0;
}
return result;
}
}
}
回答by Tom
If you want to loop over allelements of a collection, then don't use a fixed value like the 5
here:
如果要循环遍历集合的所有元素,请不要使用像5
这里这样的固定值:
System.out.println("List of instances");
for (int i = 0; i < 5; i++) {
Use arr.length
instead.
使用arr.length
来代替。
This also applies to this line:
这也适用于这一行:
for (int j = 0; j < 5; j++) {
5 might be right, since the array length is 6
and you want to terminate before the last index, but this code will break if you use a larger array. Use arr.length - 1
instead of 5
.
5 可能是正确的,因为数组长度是6
并且您想在最后一个索引之前终止,但是如果您使用更大的数组,此代码将中断。使用arr.length - 1
代替5
。
This line compares an array element with itself:
此行将数组元素与其自身进行比较:
int result = arr[i].compareTo(arr[i]);
Therefore result
will always be 0
. Change it to either:
因此result
将永远是0
。将其更改为:
int result = arr[i].compareTo(arr[j]);
or:
或者:
int result = arr[j].compareTo(arr[i]);
Try both approaches to see the difference between them.
尝试这两种方法,看看它们之间的区别。
In your fix above, you're comparing the elements on index i
and j
. Therefore you should change this code:
在上面的修复中,您正在比较 indexi
和上的元素j
。因此,您应该更改此代码:
if (result == -1) {
tempVar = arr[j + 1];
arr[j + 1] = arr[i];
arr[i] = tempVar;
}
to use the correct index of j
:
使用正确的索引j
:
if (result == -1) {
tempVar = arr[j];
arr[j] = arr[i];
arr[i] = tempVar;
}
Your current code compares the elements of i
and j
(well, not j
due to the bug, but you meant that), but your swapping different elements due to the different index j+1
.
您当前的代码比较了i
and的元素j
(好吧,不是j
由于错误,而是您的意思),但是由于不同的 index 交换了不同的元素j+1
。