java - Calling a remove() on every element to empty an ArrayList -


i'm trying write code removes movies arraylist, blanking can stuff more movies later.

i'll start code:

 for(int index = 0; index < movies.size(); index++){             removemovie(movies, movies.get(index));             } 

everytime loop runs, increment index , movies.size() should decrease. need keep movies.size() consistent while still representing original arraylist size. so, want "index < arraylistsoriginalsizehereeventhoughmyforloopisdecreasingitssizebyremovingmovies"


here's tried now:

int tempmoviesize = movies.size();         for(int index = 0; index < tempmoviesize; index++){             removemovie(movies, movies.get(index));             tempmoviesize += 1;             } 

this doesn't work though because getting outofbounds exception. should maintain size of tempmoviesize. (it goes down 1 because movie object removed, , incremented one, canceling out , keeping @ original value (in case 8).)

int size = movies.size();  int index=0; while(index<size) {     removemovie(movies, movies.get(index));      index++; } 

also, can use arraylist.remove(index) method remove elements 1 one

http://www.tutorialspoint.com/java/util/arraylist_remove.htm

int size= movies.size();  int index=0;      while(index<size)     {         movies.remove(index);          index++;     } 

by way, if want remove elements list can use

movies.clear(); 

or

movies.removeall(); 

also, here fantastic explanation of how clear() , removeall() methods different. since beginner, think in understanding concepts.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -