你会检查我的方法,让我知道我做错了什么?谢谢:)String.compareTo方法比较
public static void sortByVehicleMakeModel(Vehicle[] vehicles) {
boolean swapped = true;
for(int y = 0; y < vehicles.length && swapped; y++) {
swapped=false;
for(int x = 0; x < vehicles.length - (y+1); x++) {
if(vehicles[x].getMake() && vehicles[x].getModel().compareTo(vehicles[x + 1].getMake() && vehicles[x].getModel())) {
swap(vehicles, x, x + 1);
swapped=true;
}
}
}
}
我的错误是在第二条语句.compareto() 操作& &未定义的参数类型java.lang.String中,java.lang.String中
但是,此代码工作得很好:
public static void sortByOwnerName(Vehicle[] vehicles) {
boolean swapped = true;
for(int y = 0; y < vehicles.length && swapped; y++) {
swapped=false;
for(int x = 0; x < vehicles.length - (y + 1); x++) {
if(vehicles[x].getOwner().getName().compareTo(vehicles[x + 1].getOwner().getName())> 0) {
swap(vehicles, x, x + 1);
swapped=true;
}
}
}
}
该方法称为sortByVehicleCost,但您正在与getMake()&getModel()进行比较。你能澄清你想达到的目标吗? – threenplusone
如果您只是寻求建设性的批评,您可能想尝试http://codereview.stackexchange.com/。如果它没有按照你期望的方式工作,或者你不明白它的工作方式,那么详细说明你正在得到什么,什么是不可预料的,你期待什么等。 – EdC
mytypo,对不起! – NilR