1

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

2
  • 2
    No, you don't. Either run the loop until movies.size() is 0, always removing the first element, or better yet, use a Iterator and it's remove functionality Commented Oct 31, 2015 at 1:41
  • 1
    You're over-complicating it. It doesn't calculate tempMovieSize every time it goes around the loop. Commented Oct 31, 2015 at 1:42

4 Answers 4

2

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();
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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.

Comments

0

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.

1 Comment

Replace index with 0 (if I'm not mistaken). Every time an element is removed, the next element moves takes its position

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.