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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:10:30+00:00 2026-06-10T08:10:30+00:00

ATTENTION: I CANNOT know if doSomething will remove the element or not. This is

  • 0

ATTENTION: I CANNOT know if doSomething will remove the element or not. This is an exceptional case that my data structure needs to handle.

My problem is simples:

int size = list.size();
for(int i = 0; i < size; i++) {
   MyObj mo = list.get(i);
   mo.doSomething();
}

Now if doSomething() remove mo from the list, I eventually get an ArrayIndexOutOfBounds because the list has now shrunk.

What data structure should I use to allow iteration with the possibility of removing? I can NOT use an iterator here, in other words, I can NOT make doSomething return a boolean and call iterator.remove(). The data structure has to somehow handle the situation and continue to iterator through the rest of the elements still there.

EDIT: I CANNOT know if doSomething will remove the element or not. This is an exceptional case that my data structure needs to handle.

Part II => Making a smart listeners notifier to avoid code duplication everywhere

  • 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-10T08:10:32+00:00Added an answer on June 10, 2026 at 8:10 am

    You can use an ArrayList, for example, as long as you update the index and size when something is removed.

    List<MyObj> list = new ArrayList<MyObj>();
    int size = list.size();
    for(int i = 0; i < size; i++) {
        MyObj mo = list.get(i);
        mo.doSomething();
        if (size > list.size()) {
            size = list.size();
            i--;
        }
    }
    

    This only works if the item removed is the last one examined. For other changes to the list you will have to have more complicated logic.

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

Sidebar

Related Questions

InvalidArgumentException widget attention does not exist When I remove attention, captcha gives the same
Someone brought this article to my attention that claims (I'm paraphrasing) the STL term
Like this site Pay attention to your address bar.
It came to my attention a while ago that some people were posting images
Hi guys thanks for pay attention to this. Well im trying to replace every
A friend brought this to my attention, and after I pointed out an oddity,
A student in my class brought this to my attention, and I didn't have
I've been learning a bit about canvas and something that caught my attention (mostly
I know, I know... Eric Lippert's answer to this kind of question is usually
A reader has just brought to my attention that IE7 on Windows XP Professional,

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.