我有个问题。我正在为带有鱼的生态系统编写代码,并且正在尝试将新的 Fish 对象添加到现有的鱼 ArrayList 中。但是,当我检查 ArrayList 它原来是空的?想法?
public void addFish(Fish f){
ArrayList<Fish> fishplusone = new ArrayList<Fish>(fish.size());
if (landscape[f.getRow()][f.getCol()] != ROCK) {
for (Fish otherfish: this.fish) {
if(otherfish.getRow() == f.getRow() && otherfish.getCol() == f.getCol()) {
throw new IllegalFishPositionException(IllegalFishPositionException.TWO_FISH_IN_ONE_PLACE);
}
else {
fishplusone.add(otherfish);
}
fishplusone.add(f);
fish = fishplusone;
}
}