FAQ: Learn Java: Loops - Removing Elements During Traversal

This community-built FAQ covers the “Removing Elements During Traversal” exercise from the lesson “Learn Java: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Build Basic Android Apps with Java

Learn Java

FAQs on the exercise Removing Elements During Traversal

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I spent a quite a lot of time to solve this step.
For those who have the same questions:

  • How does the machine know, that lunchContainer == lunchBox? We have this method removeAnts. In main() we are passing an argument to it (lunchContainer) and if you look above, where we set up this removeAnts method, it takes in an argument - lunchBox. I guess this is how the machine probably knows, that lunchBox and lunchContainer are the same. Those who have more knowledge and experience are more than welcome to comment my explanation.
  • No, there’s nothing wrong with Codecademys evaluation system. Watch out for those infinite loops like the lesson warns. I mean really watch out for those! If you have conditions in your code (and you have), ask yourself whether the loop will end eventually in case of both conditions. I never thought I’ll fall for that. But I did.

i have this same question as well. very confusing just like the exercise before this one :frowning:

I have the some question as well, I am not connecting the dots on where lunchBox came from.

same, guess it all comes down to what the line:

public static ArrayList removeAnts(ArrayList lunchBox)


I’m struggling myself with using variables between methods. But I think this is how it works:

lunchContainer = removeAnts(lunchContainer);

If you have “variable = method()” it sets the variable to whatever the method returns. removeAnts() returns lunchBox, so lunchContainer = lunchBox.

So I think this is how the whole thing works:

  1. lunchContainer is initialized in the main() method.
  2. The removeAnts() method is called with lunchContainer as the parameter.
  3. The removeAnts() method is set to take an ArrayList (in this case lunchContainer) as a parameter and “copy” it to a new ArrayList called lunchBox.
  4. removeAnts() removes all instances of “ant” from the lunchBox ArrayList, thanks to our awesome loops.
  5. removeAnts() returns lunchBox.
  6. Back to the main method where removeAnts() was called: lunchContainer is changed to = the updated lunchBox ArrayList (with ants removed).

I actually think this exercise has helped me more with working out how this stuff works than how loops work since loops seem much easier.


So i was really Struggling to understand how to do this one and decided to search else where to find a better way of it being explained… I came across a far superior way of doing it which made me wonder why we are being taught such outdated methods,

The solution i found was simply to use :

// Add your code below
lunchBox.removeIf( lunch → lunch.contains(“ant”));
return lunchBox;

which worked and passed the test, so i dont understand why we are learning the other longer syntax way, please explain.

Based on some research, I don’t understand why the code is structured this way:

for (int i = 0; i < lunchBox.size(); i++) {
      if (lunchBox.get(i) == "ant"){

Specifically, why would the syntax be:


why would it not be this:


Because I thought that the argument for the .remove() function was supposed to be an int representing the index of the ArrayList item you are supposed to be removing. In this scenario, lunchBox.get(i) would return a String, which should make .remove() error out.

So what am I missing?

1 Like

Really glad you brought this up and it has got me very confused! Has this been brought up in a previous lesson?

Welcome back to the forums!

What are you referring to here?

In the first case which is


. The output from


would give you a String as you have rightfully interpreted. Upon which the whole expression becomes

lunchBox.remove(“String Value”);

. This is valid for the remove method of the ArrayList class to have and it then removes the appropriate value from the lunchBox arraylist.

In the second case


the i represents a integer and the code becomes


(the value depends on the value of i). This is also a valid syntax in the remove method of ArrayList class and so it removes the element in that particular index.

In conclusion both the syntax are valid and this is demonstrated in the lesson for remove method in the LearnJava: ArrayLists subsection of Arrays and ArrayLists section.