# ArrayList problem

• 12-17-2011, 04:53 AM
sharingan90
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 :(think):
[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;
}
}

}
• 12-17-2011, 04:56 AM
sharingan90
Re: ArrayList problem
• 12-17-2011, 01:33 PM
2by4
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?
• 12-17-2011, 01:51 PM
sharingan90
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)
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.
• 12-17-2011, 02:00 PM
2by4
Re: ArrayList problem
Quote:

Originally Posted by sharingan90
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)
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.
• 12-17-2011, 02:10 PM
sharingan90
Re: ArrayList problem