java - Some elements are just magically not removed from ArrayList -
ok here's block of code wrote :
public arraylist<location> possiblemoves() { arraylist<location> a1 = new arraylist<location>(); // array contain possible locations board testb = new board(); // test board test if locations valid or not // locations have x & y coordinates a1.add(new location(getcurrentlocation().getx() - 1, getcurrentlocation().gety() + 1)); a1.add(new location(getcurrentlocation().getx() + 1, getcurrentlocation().gety() - 1)); a1.add(new location(getcurrentlocation().getx() - 1, getcurrentlocation().gety() - 1)); a1.add(new location(getcurrentlocation().getx() + 1, getcurrentlocation().gety() + 1)); (int = 0; < a1.size(); i++) { try { tower testtower = testb.getgrid()[a1.get(i).getx()][a1.get(i).gety()]; }catch(arrayindexoutofboundsexception e) { a1.remove(a1.get(i)); } } return a1; }
when remove element, position of following ones decreased. i
. and, can use remove(int)
.
for (int = 0; < a1.size(); i++) { try { tower testtower = testb.getgrid()[a1.get(i).getx()][a1.get(i).gety()]; } catch(arrayindexoutofboundsexception e) { a1.remove(i--); } }
Comments
Post a Comment