Primitive types are not allowed in ArrayList, source. Partial solution: you can wrap prim.types such as int to Integer to form an extra class but a side effect. I want to index data, is there some substitute for ArrayList that allows primitive types?
views:
342answers:
9is there some substitute for ArrayList that allows primitive types?
Unfortunately not. Generics in Java does not support the use of primitive type parameters. The type parameter must be a class.
One must use one of the wrapper classes, such as Integer
in order to include values which are originally stored in a primitive type.
Yes write your own implementation. java.util.ArrayList has an Object[] within. You write your primitive arraylist something like
MyIntArrayList having a int[]
MyFloatArrayList having a float[]
etc.,
In fact, you can copy the entire JFC implementation of ArrayList and change the Object[] to the primitive array. Ofcourse you would want to remove all the generics and many sophisticated/generic checks, but the core logic remains the same such as array copy, array size growth etc.,
Generics in Java, and the collection framework as well, operate on Objects, not on primitives. I'm curious as to why using the object versions (like Long and Integer) is not sufficient for your needs. Is this due to performance concerns?
What version of Java are you using?
Since 1.5 autoboxing makes this issue moot.
You can do this:
List<Integer> x = Arrays.asList(1, 2, 3);
x.get(0) == 1; // this is true **WARNING** comments have pointed out this only works for Integers -128 to 127 due to integer caching. Use .equals
int foo = x.get(1); // this doesn't need a cast
x.add(4); // this doesn't need to be wrapped
There is no data structure that does exactly what you ask. Is there a reason you are opposed to using wrapper classes like Character
or Integer
? Of course, if you simply want to index data in an efficient way, you can always use a basic array like int[]
.
You can also use a third-party library, see: http://stackoverflow.com/questions/1301588/java-vector-or-arraylist-for-primitives
If you are using Java > 1.5, you can just use the autoboxing, and all will work without problem. In other cases, you can use the classes from Commons Primitives, like ArrayIntList
It doesn't support primitives directly, but you can still use them thanks to "autoboxing"
Working sample:
import java.util.List;
import java.util.ArrayList;
public class TestInt {
public static void main( String [] args ) {
List<Integer> index = new ArrayList<Integer>();
index.add(100);
index.add(200);
index.add(300);
boolean b;
b = index.contains(100);
System.out.println( b );// prints true
b = index.contains(60);
System.out.println( b );// prints false.
}
}
So you should not need a substitute.
But if you still insist you can use: Apache commons primitives but see Pyrolistical warning
If you are not restricted to specific implementations such as ArrayList, you may find the trove library usefull.
It has implementations of all basic java collections, but with primitive data types inside.