I'm trying to write code that removes all movies from an ArrayList, blanking it so I can stuff more movies into it later.
I'll start with the code:
for(int index = 0; index < movies.size(); index++){
removeMovie(movies, movies.get(index));
}
Everytime the loop runs, it will increment index and movies.size() should decrease. I need to keep movies.size() consistent while still representing the original ArrayList size. So, I want it to be like "index < ArrayListsOriginalSizeHereEvenThoughMyForLoopIsDecreasingItsSizeByRemovingMovies"
Here's something I tried just 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 I am getting an outofbounds exception. It should maintain the size of tempMovieSize. (It goes down by one because a movie object is removed, and is incremented by one, canceling it out and keeping it at the original value (in this case 8).)
movies.size()is0, always removing the first element, or better yet, use aIteratorand it'sremovefunctionalitytempMovieSizeevery time it goes around the loop.