你會檢查我的方法,讓我知道我做錯了什麼?謝謝:)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