We are about to switch to a new forum software. Until then we have removed the registration on this forum.
heey, i want to 'delete' my arraylist, to clear it i've this but that doesn't work:
for (int i = 0; i < x_waarden.size(); i ++) {
x_waarden.remove(0);
y_waarden.remove(0);
}
i've 2 arraylists, x_waarden and y_waarden, both the same size
Answers
Hey, I haven't really looked at why your code is wrong, but I just recently had to do the same thing in one of my projects. Here is the code I used to empty out an arraylist:
So I start from the end and work my way down. Hope this helps!
~Mikeshiny
ArrayList has a clear() method:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
void clear() Removes all of the elements from this list.
Method clear() got the added benefit of avoiding redundant instantiations and spare the GC! :-j
GoToLoop is correct, ArrayList has a clear() method that you should probably use. Alternatively, you could just reinitialize the ArrayList.
The reason your code doesn't work is the way you're looping. Try printing out the value of i and the current size of the ArrayList every iteration of the loop.
Let's say your ArrayList starts out at size 3. During the first iteration, i is 0 and size is 3. Second iteration, i is 1 and size is 2. Third, i is 2 and size is 1. Uh oh, i is greater than size, so your loop exits, and your ArrayList is not completely cleared.