Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:03:39+00:00 2026-05-29T16:03:39+00:00

Currently I am writing a program that must iterate through and arraylist inside of

  • 0

Currently I am writing a program that must iterate through and arraylist inside of a for loop which looks like this.

List<Integer> currentLevel = new ArrayList<Integer>();
List<Integer> nextLevel = new ArrayList<Integer>();
Iterator<Integer> it = currentLevel.iterator();
currentLevel.add(1);

for(x=0;x<20;x++){
    while(it.hasNext()){
        int element = it.next();
        nextLevel.add(element*2);
        if(element%6 == 4){
            nextLevel.add((element-1)/3);
        }
    }
    currentLevel.clear();
    currentLevel.addAll(nextLevel);
    nextLevel.clear();
}

With this code it seams that it only goes through the while loop once. Is this because after the first loop it only adds one number to the nextLevel array and the currentLevel array has the same amount of indexes as before? Or is it something that I am leaving out? If I try to add additional elements to the nextLevel array after the while loop it gives me an error at the line

int element = it.next();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:03:40+00:00Added an answer on May 29, 2026 at 4:03 pm

    You’re entering the for loop several times, but can only enter the while loop on the first pass through the for loop.

    The iterator, it is defined outside the for loop and only assigned once.

    The first time through the for loop you enter a while loop:

    while(it.hasNext()){
        int element = it.next();
        ...
    }
    

    which completely exhausts the iterator. Every subsequent time through the for loop, it.hasNext() is false, so the while loop does nothing.

    You’re also trying to re-use nextLevel and currentLevel, but those are only assigned once. So currentLevel will only contain the last set of elements added to nextLevel.

    If I try to add additional elements to the nextLevel array after the while loop it gives me an error at the line

    You are probably getting a concurrent modification exception. You can not use an iterator for a list after you modify it. https://stackoverflow.com/a/1496206/20394 explains how to deal with these problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a program that needs to compare each file in an ArrayList
I'm currently writing a program that has debug output strewn throughout it. This is
I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference.
Backgorund I am currently writing a program that allows a user to select a
I am currently writing a program which is having a lot of code duplication
I am currently writing a program that I need to send to a friend
I am currently writing a program that takes other programs, copies them, and stores
I am currently writing a program which takes user input and creates rows of
I am writing a program that must communicate with a PHP / web based
I'm currently writing up some CPPunit tests for a program that tests a hardware

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.