Results 1 to 6 of 6
  1. #1
    sharingan90 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default ArrayList problem

    Hello everyone, i;ve got this exercise which actually does a simulation of a train and i'm stack because of an arraylist issue IndexOutOfBoundsException.
    This is the part that there is a problem if you can find the mistake i would be really gratefull
    [code]
    if(station!=1)
    {
    if(check==true)
    {
    int size=ListWaggon.size();
    for(int i=0;i<size;i++)
    {
    if(ListWaggon.get(i).isTicket()==false)
    {
    ListWaggon.remove(i);
    ninjas+=1;
    }
    }

    }
    Last edited by sharingan90; 12-17-2011 at 12:44 PM.

  2. #2
    sharingan90 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: ArrayList problem

    if you need more information tell me :S

  3. #3
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: ArrayList problem

    When you remove an item from a list, do you leave a "hole"?

    For example, does the last item stay where it is?

  4. #4
    sharingan90 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: ArrayList problem

    Problem Solved.
    2by4 with the ArrayList when you remove an item there is not a hole(this is the good about it).
    By the way this is the fixed version (for anyone who got a similar problem)
    Java Code:
                if(check==true)
                {
                    int size=ListWaggon.size();
                    for(int i=size-1; i!=-1; i--)
                    {
                        if(ListWaggon.get(i).isTicket()==false)
                        {
                            ListWaggon.remove(i);
                            ninjas+=1;
                        }
                    }
    
                }
    The problem was that the array starts from 0 but ListWaggon.size() counts how many items there are in the list.
    So to use it in for you have reduce it by 1. Also for(int i=0; i<ListWaggon.size()-1; i++) doesn't work you have to do it the other way as i did.

  5. #5
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: ArrayList problem

    Quote Originally Posted by sharingan90 View Post
    Problem Solved.
    2by4 with the ArrayList when you remove an item there is not a hole(this is the good about it).
    By the way this is the fixed version (for anyone who got a similar problem)
    Java Code:
                if(check==true)
                {
                    int size=ListWaggon.size();
                    for(int i=size-1; i!=-1; i--)
                    {
                        if(ListWaggon.get(i).isTicket()==false)
                        {
                            ListWaggon.remove(i);
                            ninjas+=1;
                        }
                    }
    
                }
    The problem was that the array starts from 0 but ListWaggon.size() counts how many items there are in the list.
    So to use it in for you have reduce it by 1. Also for(int i=0; i<ListWaggon.size()-1; i++) doesn't work you have to do it the other way as i did.
    But you are not using size() in the loop. You have saved its value in the size variable.

    The problem is that when you remove an item, no hole is left in the list; all the items above the item removed shift down one position. So, for example, there is now a null where the last item previously was. So when you have removed half the items in your list, you will start encountering nulls because the other items have been shifted half way down by then.

    But your solution is absolutely the correct approach! (I was wondering if you were first going to remove the first item "size number of times". That would also work but is less efficient. :-)) But you have jumped straight to the correct solution, so, well done.

  6. #6
    sharingan90 is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default Re: ArrayList problem

    Your question made me find the answear but i had not fully understand how it works.
    Now thnx to you i have. :)

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. Problem- arraylist
    By whizzz in forum New To Java
    Replies: 3
    Last Post: 12-05-2010, 01:13 AM
  3. Arraylist problem
    By keo in forum New To Java
    Replies: 3
    Last Post: 05-28-2010, 10:38 AM
  4. ArrayList problem
    By brian.baert in forum New To Java
    Replies: 8
    Last Post: 03-06-2010, 01:49 PM
  5. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 05:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •