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

  • SEARCH
  • Home
  • 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 8973931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:33:42+00:00 2026-06-15T18:33:42+00:00

Need some clarification about java.util.list . I am using eclipse for development. I wrote

  • 0

Need some clarification about java.util.list. I am using eclipse for development.
I wrote this code

public static void main(String[] asdf){
        List<Integer> lst = new ArrayList<Integer>();
        for(int i=0;i<10000;i++){
            lst.add(i);
        }

        System.out.println(lst.size());

        for(int i=0;i<10000;i++){
            if((i%50)==0){
                lst.remove(i);
            }           
        }
        System.out.println(lst.size());

    }

But when i run this code it gives exception

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 9850, Size: 9803
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.remove(ArrayList.java:445)
    at com.ilex.reports.action.rpt.CNSReports.main(CNSReports.java:301)

one more this to note is
enter image description here

Then i did one change in code i-e iterated 2nd loop till 5000 only and it worked fine

enter image description here

Questions are
Why giving IndexOutOfBoundsException ?

What is that modCoutn ?

Do any of this thing become reason for memory Leak if yes how to resolve it ?

Thanks in advance.

  • 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-15T18:33:42+00:00Added an answer on June 15, 2026 at 6:33 pm

    Removing elements from a list makes it smaller. Your second loop runs until 10000, but the list will have shrunk to less than 10000 by the time it gets there.

    In fact, if your intent is to remove all multiples of 50, you could loop backwards from 10000 to 0, with a step size of 50 and avoid this problem, and be faster.

    for (int i=9950; i>=0; i-=50){
        lst.remove(i);
    }
    

    Note that your current approach, if your intent is to remove the multiples of 50, won’t work, since after the first remove the invariant that the value at each index is the index, no longer holds.

    modCount is an internal variable that ArrayList uses to detect whether it is changed in reference to any Iterators over it. It basically counts all modifications to the List. An Iterator keeps its own count, and checks whether it remains in sync with the List.

    Your code is not causing any memory to leak. In java memory ‘leaks’ if Objects that are no longer used are still referenced so they cannot be garbage collected. But since everything in the example is passed outside the scope of the method, everything can be gc’d once the method is left. (and since it is the main method, the vm will stop running and release its memory too)

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

Sidebar

Related Questions

I came across the following code and I need some clarification about this code.
I need some clarification about the behaviour of svn switch. I'm using SVN version
I've read several threads about this topic and need some clarification on a few
I'm in need of some clarification. I've been reading about REST, and building RESTful
I need some clarification on this subject as I just ran into an issue
I would need some clarification about JPA/Hibernate inheritance. Say I have an abstract Fruit
I need some clarification about Spring 3.0 MVC and @ModelAttribute annotated method parameter. I
I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
I just need some clarification on variables A normal variable has 2 parts to
Just need some quick clarification I have 2 Queries in my Access Database that

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.