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 7863547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:31:04+00:00 2026-06-02T23:31:04+00:00

I am searching through a ArrayList and doing a compare with 2 iterators. I

  • 0

I am searching through a ArrayList and doing a compare with 2 iterators. I am writing out values to a String buffer that will eventually be a XML output. As I parse through the values I am checking for matching itemIds. The matches are normally parts and drawings. There can be many drawings to a part. For my XML I have to know the type and name of all the matches and append the values together.

Using this ArrayList:

itemId type name

1000 part hammer
1001 part nail
1000 dwg semantic
1002 part ruler

My sample XML output would roughly look like :

<Master itemId=1000 type=part name=hammer>
  <Extra type=dwg name=semantic>
</Master>
<Master itemId=1001 type=part name=nail>
</Master>
<Master itemId=1002 type=part name=ruler>
</Master>

This is my first loop

while (theBaseInterator.hasNext()){
     ImportedTableObjects next = theBaseInterator.next(); 
     currentEntry = next.identiferId;
     currentType = next.typeId;
     currentDatasetName = next.nameId;
     compareInterator = tArray.listIterator(theBaseInterator.nextIndex());
     compareEntriesofArray(currentEntry, currentType, currentDatasetName, compareInterator); <======= calling method for 2nd loop and compare check
  }

I written a method for the second loop and compare

private void compareEntriesofArray(Object currentEntry, Object currentType, Object currentDatasetName, ListIterator<ImportedTableObjects> compareInterator)
object nextEntry;   
while (compareInterator.hasNext()) {
    ImportedTableObjects next = compareInterator.next();
    nextEntry = next.identiferId;
    if(nextEntry.equals(currentEntry)) { 
    compareInterator.remove();
    }   

When it finds a match I am trying to remove the matching entry from the list. There is no need to re-check entries that has been matched. So when the 1st loop continues down the list – it does not have to check that entry again.

But of course I am getting the ConcurrentModificationException. I fully understand why.
Is there a way that instead of trying to remove the entry, I can some how mark it with a boolean or something, so when the first loop gets to that entry in the list it knows to skip it and go to the 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-06-02T23:31:06+00:00Added an answer on June 2, 2026 at 11:31 pm

    Add all elements you want to remove into a new List.

    After iterating, call:

    coll1.removeAll (coll2);
    

    Not with iterators, and their hasNext/next, but with Lists, you can iterate with a for-loop from top to bottom. removing element (7) bevore visiting element (6) and so on has never been a problem for me, but I haven’t seen it being recommended.

    Here complete code

    import java.util.*;
    
    public class GuessGame 
    {
        public static void main ( String [] args )
        {
            char [] ca = "This is a test!".toCharArray ();
            List <Character> ls = new ArrayList <Character> ();
            for (char c: ca)
                ls.add (c);
    
            show (ls);
            // first method: remove from top/end and step backwise:
            for (int i = ls.size () - 1; i >= 0; --i)
            {
                char c = ls.get (i); 
                if (c == 'i' || c == 'a' || c == 'e')
                    ls.remove (i); 
            }
            show (ls);
    
            // second approach: collect elements to remove ...
            ls = new ArrayList <Character> ();
            for (char c: ca)
                ls.add (c);
            show (ls);
            // ... in a separate list and 
            List <Character> toRemove = new ArrayList <Character> ();
            for (char c: ls)
            {
                if (c == 'i' || c == 'a' || c == 'e')
                    toRemove.add (c); 
            }
            // ... remove them all in one go:
            ls.removeAll (toRemove);
            show (ls);
        }
    
        private static void show (List <Character> ls)
        {
            for (char c: ls)
                System.out.print (c + " ");
            System.out.println ();
        }   
    }
    

    Output:

    T h i s   i s   a   t e s t ! 
    T h s   s     t s t ! 
    T h i s   i s   a   t e s t ! 
    T h s   s     t s t ! 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching for the past through days, trying to figure out how
I am doing on a project for searching through an image database, and when
I'm trying to develop an application that will simulate a route through Google's Navigation
I am recursively searching through directories, and I just need to make sure that
There are 2 fields that I rely on for searching through the table: field1,
I'm searching through a list of values, and want to have it return some
I am searching through a string trying to count the number of occurrences of
I am searching through a tree to find a value that is passed. Unfortunately,
I have tried searching through related answers but can't quite find something that is
After searching through many PHP frameworks to figure out which to learn and use,

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.