1

I have the oddest problem, probably with a simple solution.

I have created and initialized a list and then proceeded to create 4 objects of the list's type. In the constructor of these they place themselves in the list. Or at least are supposed to. I always get an out of bound exception and I cant figure out why. I set the list to have a size of 402 (for all possible VK values) but in the console and debug it always says it has size 0, no matter how large or empty I set it too....

public class InputHandler implements KeyListener
{

public static List<Key> keyList = new ArrayList<Key>(KeyEvent.KEY_LAST);

public Key up = new Key(KeyEvent.VK_UP);
public Key down = new Key(KeyEvent.VK_DOWN);
public Key left = new Key(KeyEvent.VK_LEFT);
public Key right = new Key(KeyEvent.VK_RIGHT);


public class Key
    {

    public int keyCode;

    public Key(int defaultCode)
    {
        this.keyCode = defaultCode;
        keyList.add(keyCode,this);
    }

    public Key reMapKey(int newKey)
    {
        keyList.remove(keyCode);
        keyList.set(newKey, this);
        this.keyCode = newKey;
        return this;
    }

}

}

There is more to the code but I attempted to SSCCE it.

The only info of value from the console is this:

Exception in thread "RogueLoveMainThread" java.lang.IndexOutOfBoundsException: Index: 38, Size: 0

Much apologies for my stupidity

  • If this is related to Swing, use KeyBindings instead. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html). – Eng.Fouad May 08 '13 at 19:40
  • Possible duplicate of [ArrayList initial capacity and IndexOutOfBoundsException](http://stackoverflow.com/questions/11908037/arraylist-initial-capacity-and-indexoutofboundsexception) – Raedwald Jan 26 '16 at 22:14

2 Answers2

5

You've created a new ArrayList with a capacity of 402, but it's still got a size of 0 after the constructor call. From the docs of the constructor call you're using:

public ArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity - the initial capacity of the list

And from the docs of ArrayList itself:

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically.

The capacity is not the size.

So, some options:

  • Populate the list with null entries until you have got the size you want
  • Use an array instead of a list (after all, you only need it to be a fixed size, right?)
  • Use a Map<Integer, Key> instead
Community
  • 1
  • 1
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
4

keyList.add(keyCode, this) inserts at the position of keyCode. As your list is still empty (size 0) you cannot insert at any position position greater than 0.

You might want to have map codes to keys, do you? There is a Map<K, V> for this:

static Map<Integer, Key> keyMap = new TreeMap<>();

public Key(int defaultCode) {
    keyMap.add(keyCode, this);
}

If you need a key by its code, you can receive it from the Map as follows:

keyMap.get(keyCode);
Matthias Meid
  • 12,080
  • 6
  • 41
  • 73