Daniel Bingham Daniel Bingham - 1 year ago 167
Java Question

Java Vector or ArrayList for Primitives

Is there an expandable array class in the Java API equivalent to the

class that can be used with primitives (int, char, double, etc)?

I need a quick, expandable array for integers and it seems wasteful to have to wrap them in the
class in order to use them with
. My google-fu is failing me.

Answer Source

There is unfortunately no such class, at least in the Java API. There is the Primitive Collections for Java 3rd-party product.

It's pretty dangerous to use auto-boxing together with existing collection classes (in particular List implementations). For example:

List<Integer> l = new ArrayList<Integer>();

l.remove(4); //will throw ArrayIndexOutOfBoundsException
l.remove(new Integer(4)); //what you probably intended!

And it is also a common source of mysterious NullPointerExceptions accessing (perhaps via a Map):

Map<String, Integer> m = new HashMap<String, Integer>();
m.put("Hello", 5);
int i = m.get("Helo Misspelt"); //will throw a NullPointerException
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download