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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:00:00+00:00 2026-05-15T21:00:00+00:00

Can somebody explain to me whats wrong with the below piece of code ?

  • 0

Can somebody explain to me whats wrong with the below piece of code ?

public static void main(String[] args) {
 List<String> l = new ArrayList<String>();
  l.add("1");
  l.add("2");
  l.add("3");
  l.add("4");

  for (int i = 0; i < l.size(); i++) {
   if(l.get(i).equals("1"))
    l.remove(l.get(i));
   else
    System.out.println(l.get(i));
  }
 }

gives me an output of [3.4] instead of [2,3,4] .. Wheres my [2] ? I am a lil confused with this behavior of the List.. Great if somebody could explain..

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-05-15T21:00:01+00:00Added an answer on May 15, 2026 at 9:00 pm

    The reason is:
    When i = 0 you remove the first element and i becomes 1
    When i = 1 you are now at the third element, because you have shifted everything down by 1 so you write “3”
    When i = 2 you write the third element which is “4”

    Giving you an output of “3”,”4″

    An alternate implementation might be:

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class Item {
        public static void main(String[] args) {
            List<String> l = new ArrayList<String>();
            l.add("1");
            l.add("2");
            l.add("3");
            l.add("4");
    
            Iterator<String> iter = l.iterator();
            while(iter.hasNext())
            {
                String value = iter.next();
                if("1".equals(value))
                {
                    iter.remove();
                }
                else
                {
                    System.out.println(value);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I stumbled upon this code: static void Main() { typeof(string).GetField(Empty).SetValue(null, evil);//from DailyWTF Console.WriteLine(String.Empty);//check //how
can somebody please explain what is wrong with my functions, thanks in advance: I
enter code here hello, everyone, can somebody explain what is my problem in this
Can somebody explain me what I am doing wrong? This is teh error I
Can somebody please explain with examples (of code) what is the difference between deadlock
Can somebody explain to me the following (python 2.7) Two string numbers, from a
I'm running a Django app using FastCGI and lighttpd. Can somebody explain me what
Can somebody advise me what this code does and how can I convert it
I'm a little bit confused about these two terms, can somebody explain what is
Can somebody explain what is the exact reason behind using Action<T> and Predicate<T> as

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.