3

Possible Duplicate:
How to convert List<Integer> to int[] in Java?

In Java, is below the fastest solution:

public convert(ArrayList<Integer> IntegerList) {

    int s = IntegerList.size();
    int[] intArray = new int[s];
    for (int i = 0; i < s; i++) {
        intArray[i] = IntegerList.get(i).intValue();
    }
}

?

Community
  • 1
  • 1
Sophie Sperner
  • 4,428
  • 8
  • 35
  • 55

4 Answers4

5

The fastest way is to not use an ArrayList<Integer> in the first place. Try TIntArrayList which wraps an int[], or use a int[] from the start.

If you have to use an ArrayList for some reason and you can't fix it, you are need a way which works and performance is less important.

int[] ints = new int[list.size()];
for(int i=0, len = list.size(); i < len; i++)
   ints[i] = list.get(i);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    The OP asks the question starting the problem with an `ArrayList`, in your solution you do not consider the time you spend to trasform an `ArrayList` to a `TIntArrayList`. – thermz Aug 14 '12 at 13:02
  • The reason of using ArrayList at the start is that I do not know how much elements it will contain, once I filled in the ArrayList, I need a copy for further speedup. – Sophie Sperner Aug 14 '12 at 13:02
  • TIntArrayList can be any size. Why do you need a copy? – Peter Lawrey Aug 14 '12 at 13:11
  • What is `TIntArrayList`? – Sophie Sperner Aug 14 '12 at 13:15
  • 2
    http://trove4j.sourceforge.net/javadocs/gnu/trove/list/array/TIntArrayList.html If you don't want to include an additional library, its trivial to implement. – Peter Lawrey Aug 14 '12 at 13:20
5

I cant think of any better solution than you have now with plain java. However if you use Guava, You can probably simplify it.

public convert(List<Integer> list) {
 int[] ar = Ints.toArray(list); //Ints is the class from Guava library
}
RP-
  • 5,827
  • 2
  • 27
  • 46
2
public void convert(ArrayList<Integer> IntegerList) {
        int[] intArray = new int[IntegerList.size()];
        int count = 0;
        for(int i : IntegerList){
            intArray[count++] = i;
        }
 }

UPDATE : Q. but is a foreach loop any faster/better/different from a regular for loop?
A. here

Community
  • 1
  • 1
Harmeet Singh
  • 2,555
  • 18
  • 21
  • I don't know the answer off hand, but is a foreach loop any faster/better/different from a regular for loop? I thought it was the same thing, with an easier way to write it. – Brian J Aug 14 '12 at 13:01
  • What is a "regular loop"? EDIT: ah I see, the advantage of the foreach- over the for-loop is that it probably uses an iterator instead of indices to access the list, thus not relying on the list providing get(i) in O(1). See http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work on how a foreach-loop works. – G. Bach Aug 14 '12 at 13:02
  • @BrianJ here you [go](http://stackoverflow.com/questions/256859/is-there-a-performance-difference-between-a-for-loop-and-a-for-each-loop) – Harmeet Singh Aug 14 '12 at 13:05
0
 Integer[] intArray = IntegerList.toArray(new Integer[IntegerList.size()]);
Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37