views:

3453

answers:

6

This is similar to this question: http://stackoverflow.com/questions/880581/java-convert-int-to-integer

I'm new to Java. How can i convert a List to int[] in Java? I'm confused because List.toArray() actually returns an Object[], which can be cast to nether Integer[] or int[].

Right now I'm using a loop to do so:

int[] toIntArray(List<Integer> list){
  int[] ret = new int[list.size()];
  for(int i = 0;i < ret.length;i++)
    ret[i] = list.get(i);
  return ret;
}

I'm sure there's a better way to do this.

+9  A: 

Unfortunately, I don't believe there really is a better way of doing this due to the nature of Java's handling of primitive types, boxing, arrays and generics. In particular:

  • List<T>.toArray won't work because there's no conversion from Integer to int
  • You can't use int as a type argument for generics, so it would have to be an int-specific method (or one which used reflection to do nasty trickery).

I believe there are libraries which have autogenerated versions of this kind of method for all the primitive types (i.e. there's a template which is copied for each type). It's ugly, but that's the way it is I'm afraid :(

Even though the Arrays class came out before generics arrived in Java, it would still have to include all the horrible overloads if it were introduced today (assuming you want to use primitive arrays).

Jon Skeet
+2  A: 

There is really no way of "one-lining" what you are trying to do because toArray returns an Object[] and you cannot cast from Object[] to int[] or Integer[] to int[]

neesh
You *can* cast between Object[] and Integer[] due to array covariance - but you can't cast between int[] and Integer[].
Jon Skeet
thanks for correcting me I will edit my answer to reflect what you said.
neesh
+15  A: 

The easiest way to do this is to make use of Apache Commons Lang. It has a handy ArrayUtils class that can do what you want. Use the toPrimitive method with the overload for an array of Integers.

List<Integer> myList;
 ... assign and fill the list
int[] intArray = ArrayUtils.toPrimitive(myList.toArray(new Integer[myList.size()]));

This way you don't reinvent the wheel. Commons Lang has a great many useful things that Java left out. Above, I chose to create an Integer list of the right size. You can also use a 0-length static Integer array and let Java allocate an array of the right size:

static final Integer[] NO_INTS = new Integer[0];
   ....
int[] intArray2 = ArrayUtils.toPrimitive(myList.toArray(NO_INTS));
Eddie
I was searching for this question as I remembered someone had posted the location of the commons library that would do this.
Ravi Wallau
+2  A: 
int[] toIntArray(List<Integer> list)  {
    int[] ret = new int[list.size()];
    int i = 0;
    for (Integer e : list)  
        ret[i++] = e.intValue();
    return ret;
}

Slight change to your code to avoid expensive list indexing.

I dont understand: looking up an elepment in an array is not slow. It runs in O, where n is the size of the array, ie it's not dependent on the size of the array at all or anything.In C:myarray[c] is the same as:myarray + c * sizeof( myarray[0] )... which runs *very* fast.
Hugh Perkins
The method takes List as argument, not all implementations have fast random access (like ArrayList)
arjan
@Hugh: The difference is not how the array is accessed, but how the List is accessed. `list.get(i)` performs bounds checks and stuff for each access. I don't know whether the new solution is actually better, but Mike says so at least, so perhaps it is. Edit: I forgot that Java allows indexing into a linked list (I'm used to C++'s std::list, which does not allow it). So what arjantop said about non-ArrayList is true as well; indexing isn't necessarily fast even without the bounds checks.
Lajnold
+2  A: 

In addition to Commons Lang, you can do this with Guava's method Ints.toArray(Collection<Integer> collection).

List<Integer> list = ...
int[] ints = Ints.toArray(list);

This saves you having to do the intermediate array conversion that the Commons Lang equivalent requires yourself.

ColinD
A: 

try also Dollar (check this revision):

import static com.humaorie.dollar.Dollar.*
...

List<Integer> source = ...;
int[] ints = $(source).convert().toIntArray();
dfa