java - Can't add to an array list -
i have oddest problem, simple solution.
i have created , initialized list , proceeded create 4 objects of list's type. in constructor of these place in list. or @ least supposed to. out of bound exception , cant figure out why. set list have size of 402 (for possible vk values) in console , debug says has size 0, no matter how large or empty set too....
public class inputhandler implements keylistener { public static list<key> keylist = new arraylist<key>(keyevent.key_last); public key = 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 more code attempted sscce it.
the info of value console this:
exception in thread "roguelovemainthread" java.lang.indexoutofboundsexception: index: 38, size: 0
much apologies stupidity
you've created new arraylist
capacity of 402, it's still got size of 0 after constructor call. docs of constructor call you're using:
public arraylist(int initialcapacity)
constructs empty list specified initial capacity.
parameters:
initialcapacity
- initial capacity of list
and docs of arraylist
itself:
each
arraylist
instance has capacity. capacity size of array used store elements in list. @ least large list size. elements added arraylist, capacity grows automatically.
the capacity not size.
so, options:
- populate list null entries until have got size want
- use array instead of list (after all, need fixed size, right?)
- use
map<integer, key>
instead
Comments
Post a Comment