# Thread: ArrayList problem

1. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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 01:44 PM.

2. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## Re: ArrayList problem

if you need more information tell me :S

3. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## 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. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: ArrayList problem

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)
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. Member
Join Date
Dec 2011
Posts
4
Rep Power
0

## 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. :)

#### Posting Permissions

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