views:

342

answers:

9

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?

A: 

is 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.

coobird
A: 

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.,

Bragboy
A: 

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?

Uri
+2  A: 

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
Pyrolistical
Assuming the OP is using a modern version of Java, this is really the only answer necessary.
Etaoin
using 1.5, can you give a working example? so you have a class "class Integer { int i; } for the Integer in "List<Integer>"?
HH
@HH no, Integer ( the wrapper ) already exist: ( java.lang.Integer ) here's a simple working example: http://stackoverflow.com/questions/2602038/java-substitute-for-arraylist-cos-primitive-types-not-allowed-in-arraylist/2602228#2602228
OscarRyz
@Pyrolistical: using x.get(0) is asking for trouble. As an example, Arrays.asList(1, 1, 2); x.get(0) == x.get(1) will work because of a cache of intern'd Integers, but Arrays.asList(1000, 1000, 2); x.get(0) == x.get(1) will return false because the 2 Integers are different instances. Also, x.remove( 1 ) is very ambiguous... remove element #1, or the element that equals value 1?
Trevor Harrison
crap. autobox fail =(
Pyrolistical
@Trevor I made it into a question http://stackoverflow.com/questions/2602636/why-cant-the-compiler-jvm-just-make-autoboxing-just-work
Pyrolistical
A: 

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

Justin Ardini
A: 

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

Valentin Rocher
A: 

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

OscarRyz
I would avoid letting people know about commons primitives. This is a valid Java solution only experts should use that.
Pyrolistical
@Pyrolistical how come?
OscarRyz
It is a third party library so the public class library isn't designed for it and you are introducing more concept overhead for the reader of the code. Second people tend to think OMG this is faster therefore I shall only use this! They are prematurely optimizing and the fastest thing is generally do to the simplest thing. Code isn't for computers to run fast, code is for humans to read and understand. Use the standard libraries and who knows in the future maybe a JVM will suddenly improve Collection performance. But you won't get that if you use third party libraries.
Pyrolistical
Mhhh ... I think it had it's place prior to 1.5 but now I had become somehow obsolete... It still have some useful features. I wouldn't say it's for "experts" only, but definitely not for "newbies" otherwise we wouldn't be using any library at all. Yet, your explanation is pretty valid.
OscarRyz
+2  A: 

Trove

Trevor Harrison
A: 

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.

Eyal Schneider