我在从ArrayList中删除项目时遇到了一些麻烦,特别是删除了已指定的项目并将此项目的id传递给remove()函数。
DockService service = new DockServiceImpl();
// This should remove the boat with the ID: NV 2345 KO
service.removeBoat("NV 2345 KO");
if (service.getDock().getDockedBoats().size() == 2) {
System.out.println("Test 2 - Removing a boat: Passed.");
} else {
System.out.println("Test 2 - Removing a boat: Failed.");
}
这是一个单独的类
中的ArrayList public static List<Boat> createBoatList() {
List<Boat> boatList = new ArrayList<>();
Boat firstBoat = new Boat("NV 1234 VN", 1000);
Boat secondBoat = new Boat("NV 2345 KO", 1500);
Boat thirdBoat = new Boat("NV 3053 NZ", 2000);
boatList.add(firstBoat);
boatList.add(secondBoat);
boatList.add(thirdBoat);
return boatList;
}
这是包含get set methods
的dock类 public Dock(String name, int capacity) {
this.name = name;
this.capacity = capacity;
this.dockedBoats = new ArrayList<>();
}
public List<Boat> getDockedBoats() {
return dockedBoats;
}
所以在我的删除方法中,我试过这个
private Dock dock;
public boolean removeBoat(String boatId) {
System.out.println(">>> Deleting boat.");
this.dock.getDockedBoats().remove(boatId);
return false;
}
但是删除(boatId)并没有从arraylist中删除该船,任何帮助将不胜感激
答案 0 :(得分:2)
您正在class Foo
{
public:
template<typename... Args>
requires (ConvertibleNoNarrow<Args, double> && ...)
Foo(Args&&... args) { /*...*/ }
};
中存储Boat
个对象 - 然后您尝试通过传递List
- &gt;删除一个Boat
。这不行。
您需要首先找到给定参数的匹配String
对象,然后从列表中删除该对象。
有些事情:
Boat