user5487589
user5487589

Reputation:

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

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).)

Upvotes: 1

Views: 573

Answers (4)

Dale
Dale

Reputation: 1628

Just create a while loop with all your movies in it. Then say something like

while (myMovieCollect.notEmpty()){
     removeMovie(myMovieCollect, myMovieCollect.get(0));
}

Of course this is psudeoCode but you get the idea.

Upvotes: 0

200_success
200_success

Reputation: 7582

Problem

Suppose that movies is a list of size 2. With your original code, you would first remove movies.get(0), leaving a list of size 1. The second time through, index is 1, and movies.size() is also 1, so the other movie won't get removed.

Your second attempt is even more problematic. Each time through the loop, index increases by 1, and tempMovieSize also increases by 1. Therefore, it's an infinite loop.

Remedy

As others have mentioned, movies.clear() is the simplest way to empty the movies list.

If you actually need to call removeMovie(…) on every item in the list, and you have flexibility to choose the order in which you do so, then I suggest that you iterate backwards from the end:

for (int index = movies.size() - 1; index >= 0; index--) {
    removeMovie(movies, i);
}

One advantage is that you only need to call movies.size() once. Another is that you are always removing the last element of the list. Removing the first element of an ArrayList would be inefficient, since all of the subsequent elements would need to be copied over to fill the void.

Note that I've changed removeMovie(…) to take an index, so that it can do movies.remove(index) rather than movies.remove(object). The latter would necessitate searching through the list to locate the object to be removed, which would be involve traversing the list to the end again.

Upvotes: 0

AbtPst
AbtPst

Reputation: 8008

int size = movies.size();

int index=0;
while(index<size)
{
    removeMovie(movies, movies.get(index));

    index++;
}

also, you can use the ArrayList.remove(index) method to remove elements one by 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 the way, if you just want to remove all the elements from your list you can use

movies.clear();

or

movies.removeAll();

also, here is a fantastic explanation of how the clear() and removeAll() methods are different. Since you are a beginner, i think this will really help you in understanding the concepts.

Upvotes: 1

JimN
JimN

Reputation: 3150

You are incrementing the loop index at the end of each loop iteration. But this is wrong because if you have just removed an element, you'll end up skipping the following element.

To fix this, you should not increment the loop index within an iteration in which you have just removed an element.

Edit:

Since you simply want to remove all movies:

movies.clear();

Upvotes: 2

Related Questions