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

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -