火箭类包含:canCarry(项目项)>检查该项目是否可以携带/随身携带用总重量更新权重。
currentweight,maxWeight=18 U2类是火箭包含的子级: tons项目类包含:名称要运输&重量。
在loadU2方法中,我试图访问一个项目列表,并将其添加到一个火箭中,直到到达该火箭的maxWeight为止。例如,我有216吨的物品要携带,退回12艘船的清单。
它在行java.lang.IllegalStateException中抛出iterator.remove().错误我不知道如何去做,但是它看起来不允许我在迭代时删除项目。
public ArrayList<Rocket> loadU2(ArrayList<Item> loadItems){
//list of ships
ArrayList<Rocket> U2Ships = new ArrayList<Rocket>();
for(Iterator<Item> iterator = loadItems.iterator(); iterator.hasNext();) {
//create a new ship
Rocket tempShip = new U2();
Item tempItem = iterator.next();
//loop over items check if it can be filled then remove the item that was filled.
while(tempShip.currentWeight<tempShip.weightLimit) {
if(tempShip.canCarry(tempItem)){
tempShip.carry(tempItem);
iterator.remove();
}
}
U2Ships.add(tempShip);
}
return U2Ships;
}
Exception in thread "main" java.lang.IllegalStateException
at java.base/java.util.ArrayList$Itr.remove(ArrayList.java:980)
at Simulation.loadU1(Simulation.java:35)
at Main.main(Main.java:14)
代码所做工作的简化示例:假设每艘船的maxWeight = 11吨ArrayList loadItems = 3,5,5,8,1,2,3,5吨
- Ship[1]=[3,5,1,2]
- new list to iterate over >> [5,8,3,5]
- Ship[2]=[5,3]
- new list to iterate over >> [8,5]
- Ship[3]=[8]
- new list to iterate over >> [5]
- Ship[4]=[5]
发布于 2021-05-30 14:22:18
使用listIterator而不是Iterator。
ListIterator<Book> iter = books.listIterator();
while(iter.hasNext()){
if(iter.next().getIsbn().equals(isbn)){
iter.remove();
}
}
就像这里用过的。
https://stackoverflow.com/questions/67761499
复制相似问题