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

2

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. I can't use an array as I need to do some things with remapping of the keys, I think I'll just fill it with nulls
Eh... What about the 'options' section at the end of the answer?
@BenjaminTilbury: What do you mean by "need to do some things"? If you have other methods which take a list, you can always use Arrays.asList.
@skuntsel that wasn't there when I said that :L
@JonSkeet Theres some methods that I will add later that will be much easier to do with a list rather than making an array into a list
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.