Java object not added to ArrayList -
i have problem. writing code ecosystem fish , trying add new fish object existing fish arraylist . however, when check arraylist turns out empty? thoughts?
public void addfish(fish f){ arraylist<fish> fishplusone = new arraylist<fish>(fish.size()); if (landscape[f.getrow()][f.getcol()] != rock) { (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; } }
you create new arraylist each time: arraylist<fish> fishplusone = new arraylist<fish>(fish.size());
isn't easier checks first , add existing list?
Comments
Post a Comment