Fastest way to convert ArrayList<Integer> into int[]

19,784

Solution 1

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
}

Solution 2

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);

Solution 3

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

Share:
19,784
Sophie Sperner
Author by

Sophie Sperner

Smart &amp; happy blond! ))))

Updated on June 25, 2022

Comments

  • Sophie Sperner
    Sophie Sperner almost 2 years

    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();
        }
    }
    

    ?

  • Jeffrey
    Jeffrey over 11 years
    The OP is looking for an int[], not an Integer[].
  • Pramod Kumar
    Pramod Kumar over 11 years
    You can do everything with Integer what you can do with int. thats why i posted it
  • Brian J
    Brian J over 11 years
    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.
  • thermz
    thermz over 11 years
    The OP asks the question starting the problem with an ArrayList<Integer>, in your solution you do not consider the time you spend to trasform an ArrayList<Integer> to a TIntArrayList.
  • G. Bach
    G. Bach over 11 years
    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 stackoverflow.com/questions/85190/… on how a foreach-loop works.
  • Sophie Sperner
    Sophie Sperner over 11 years
    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<Integer>, I need a copy for further speedup.
  • Harmeet Singh
    Harmeet Singh over 11 years
    @BrianJ here you go
  • Vishy
    Vishy over 11 years
    TIntArrayList can be any size. Why do you need a copy?
  • Sophie Sperner
    Sophie Sperner over 11 years
    What is TIntArrayList?
  • Vishy
    Vishy over 11 years
    trove4j.sourceforge.net/javadocs/gnu/trove/list/array/… If you don't want to include an additional library, its trivial to implement.
  • Brian J
    Brian J over 11 years
    @G.Bach Thanks for the information!
  • Brian J
    Brian J over 11 years
    @HarmeetSingh Thanks for the information!
  • Jeffrey
    Jeffrey over 11 years
    Except perform any arbitrary calculation without a loss of performance.