I get a java.lang.IndexOutOfBoundsExeption: Invalid index 9, size is 5
<p>I keep getting this exeption when I am removing and placing a item from one array-list to another.
Sometimes the index is 13 and size is 5. other times is 0 and 0. It happens at random and more often on slower devices.</p>

<pre><code>11-05 19:42:34.487: W/dalvikvm(2232): threadid=14: thread exiting with uncaught exception   (group=0x40c3ca68)
11-05 19:42:34.497: E/AndroidRuntime(2232): FATAL EXCEPTION: Thread-491
11-05 19:42:34.497: E/AndroidRuntime(2232): java.lang.IndexOutOfBoundsException: Invalid index size is 5
11-05 19:42:34.497: E/AndroidRuntime(2232):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at java.util.ArrayList.add(ArrayList.java:143)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at com.pigeoncraft.pigeoncraft.entity.Inventory.add(Inventory.java:25)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at com.pigeoncraft.pigeoncraft.screen.ContainerMenu.tick(ContainerMenu.java:62)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at com.pigeoncraft.pigeoncraft.Game.tick(Game.java:312)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at com.pigeoncraft.pigeoncraft.Game.iterate(Game.java:265)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at com.pigeoncraft.pigeoncraft.GameActivity$2.run(GameActivity.java:128)
11-05 19:42:34.497: E/AndroidRuntime(2232):     at java.lang.Thread.run(Thread.java:856)
</code></pre>

<p>Here is the code for Inventory.</p>

<pre><code>    package com.mojang.ld22.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import  com.pigeoncraft.pigeoncraft.item.Item;
import  com.pigeoncraft.pigeoncraft.ResourceItem;
import  com.pigeoncraft.pigeoncraft.Resource;

public class Inventory  implements Serializable {
private static final long serialVersionUID = -8246630353617240883L;

public List&lt;Item&gt; items = new ArrayList&lt;Item&gt;();

public void add(Item item) {
    add(items.size(), item);
}

public void add(int slot, Item item) {
    if (item instanceof ResourceItem) {
        ResourceItem toTake = (ResourceItem) item;
        ResourceItem has = findResource(toTake.resource);
        if (has == null) {
            items.add(slot, toTake); &lt;--------------------
        } else {
            has.count += toTake.count;
        }
    } else {
        items.add(slot, item);
    }
}

private ResourceItem findResource(Resource resource) {
    for (int i = 0; i &lt; items.size(); i++) {
        if (items.get(i) instanceof ResourceItem) {
            ResourceItem has = (ResourceItem) items.get(i);
            if (has.resource.name.equals(resource.name))
            {
                return has;
            }
        }
    }
    return null;
}

public boolean hasResources(Resource r, int count) {
    ResourceItem ri = findResource(r);
    if (ri == null) return false;
    return ri.count &gt;= count;
}

public boolean removeResource(Resource r, int count) {
    ResourceItem ri = findResource(r);
    if (ri == null) return false;
    if (ri.count &lt; count) return false;
    ri.count -= count;
    if (ri.count &lt;= 0) items.remove(ri);
    return true;
}

public int count(Item item) {
    if (item instanceof ResourceItem) {
        ResourceItem ri = findResource(((ResourceItem)item).resource);
        if (ri!=null) return ri.count;
    } else {
        int count = 0;
        for (int i=0; i&lt;items.size(); i++) {
            if (items.get(i).matches(item)) count++;
        }
        return count;
    }
    return 0;
}
}
</code></pre>
