Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Unexpected Behaviour of ArrayList

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unexpected Behaviour of ArrayList

    import java.util.*;
    class ArrayListDemo2
    {
    public static void main(String args[])
    {
    ArrayList al=new ArrayList();
    al.add("one");
    al.add("two");
    al.add("three");
    al.add("four");
    for(int i=0;i<al.size();i++)
    {
    System.out.println(al.remove(i));
    }
    }
    }

    Expected Output:-

    one
    two
    three
    four

    But I got

    one
    three

    What is the reason for this output?
    How to get my expected output?


  2. #2
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unexpected Behaviour of ArrayList

    import java.util.*;
    public class arraylistDemo2 {
    public static void main(String args[])
    {
    ArrayList al=new ArrayList();
    al.add("one");
    al.add("two");
    al.add("three");
    al.add("four");
    Iterator i=al.iterator();
    while(i.hasNext())
    {
    System.out.println(i.next());
    }
    }



    }

    output:
    one
    two
    three
    four


    hi i have some idea about that,iterator is used to travelling a object so you can use extact contain in arraylist
    if it is wrong sorry

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Unexpected Behaviour of ArrayList

    @both of you: Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

Similar Threads

  1. [SOLVED] Unexpected type
    By hooshdar3 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 17th, 2014, 09:14 AM
  2. unexpected output
    By sumitroy in forum Threads
    Replies: 4
    Last Post: June 30th, 2014, 07:30 AM
  3. Funny Behaviour
    By lanmonster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 28th, 2013, 05:04 AM
  4. Unexpected ArrayOutOfBoundsError
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 28th, 2010, 04:00 AM
  5. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM