我得到這個功能,這是一個private boolean
函數,檢查車庫內是否已經有相同大小的汽車。如果沒有,我將它添加到arrayList
,我已經做了printList()
類型的函數,然後遍歷arraylist
並打印出值(這很好用),但不知怎的,我的private boolean
函數似乎根本不起作用。Java比較兩個字符串似乎不起作用
繼承人我的代碼:
public class Cars {
public Cars (String size, boolean booking) {
this.carSize = size;
this.isBooked = booking;
}
public String getSize() {
return this.carSize;
}
public boolean checkBook() {
return this.isBooked;
}
private String carSize;
private boolean isBooked;
}
public class Locations {
public Locations (String curLocation) {
garage = new ArrayList<Cars>();
location = curLocation;
}
public void addCar (String size, boolean booking) {
if (garage.isEmpty() || !checkCar(size)) {
garage.add(new Cars(size, booking));
System.out.println("Car assigned " + location + " " + size);
}
}
private boolean checkCar (String size) {
for (Cars car : garage) {
System.out.println("hey");
if (size.equals(car.getSize())) return true;
}
return false;
}
private ArrayList <Cars> garage;
private String location;
}
輸入是爲以下幾點:
Car small City
Car small Redfern
Car small Redfern
輸出:
Car assigned City small
Car assigned Redfern small
Car assigned Redfern small
它不應該打印出第二雷德芬小,因爲有已經是名單內的那輛車。
順便說一下,'Cars'類不應該是複數。 – SLaks
需要你的整個代碼。 –
沒有看到你的主要方法,很難說它有什麼問題。也許你在第三行有額外的空間?你能在平等比較之前調整它嗎? – CoolBeans