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 4 of 4

Thread: Looping through ArrayList to remove elements

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Looping through ArrayList to remove elements

    I'm creating a binsort program and I ran into a problem. I created a ArrayList bucket and asked the user to input values. Let's say it has elements [1, 2, 3, 4, 5, 6, 7] and I'm using a for loop to remove it.
    for(int i = 0; i <= bucket.size(); i++)
    {
    System.out.println(bucket.remove(i);
    }
    So to my logic, it is suppose to loop starting from 0. As it loops, as long as i is less than or equal to the size of the bucket, it'll increase by 1. As that is happening, it's suppose to remove the element at that position. It does remove it but, when I print that out, it comes out as...

    1
    3
    5
    7
    2
    6
    4

    I don't understand why would it print out like that. Also, if a user inputted an even number of elements, it would return "Index out of Bounds Exception". So just wondering why is this happening to my for loop. Any help would be appreciated!


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Looping through ArrayList to remove elements

    Hello and welcome to the forums.

    I think we may need to see a bit more of your code..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Looping through ArrayList to remove elements

    Consider how the loop is working - write out what exactly is happening during the loop...remove element at index 0, 1, etc...what does the array look like after each removal? How else could you loop? Are there API methods to accomplish this?

  4. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (October 11th, 2011)

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Looping through ArrayList to remove elements

    thank you for the responses but i figured it out.

    what i was doing wrong was as i was looping through the array, i was indeed removing the element at position i but that also changed the size of my array. Which in the end, when i increases, the array size decreases. therefore, when i = 0 it removes element at position 0 which is 1 in this case, and decreases the size of the array by 1 which in this case would change from size 7 to size 6. Now when it removes element at position 1, position 1 of the array is 3 since we decreased the size of the array.

Similar Threads

  1. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  2. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  3. Duplicate elements in 2 Arraylist
    By tcstcs in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 12:56 AM
  4. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM
  5. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM

Tags for this Thread