0

I want to know how convert int[] to Integer[] in java.

Basically i am trying to convert int[] to ArrayList in java.

I found some examples but they are based on Integer object and not based on int[].

referred: Convert int[] into ArrayList

Thanks

Community
  • 1
  • 1
mahesh
  • 1,523
  • 4
  • 23
  • 39
  • 3
    Traverse each element in your `int[]` and add it to your `List`. – Luiggi Mendoza Nov 19 '13 at 15:50
  • 1
    You cannot use generics like the `List` interface and the `ArrayList` class with primitive types as `int`. You have to use the boxed type `Integer`. – Robin Krahl Nov 19 '13 at 15:51
  • 1
    is there any other way other than this. because if I use traverse ,this will be in loop which i am going to iterate over 10k times,not sure how it is going to impact – mahesh Nov 19 '13 at 15:52
  • 1
    use [`Arrays.asList()`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)) and then use it to initialize an `ArrayList` if you need additional functionality – vandale Nov 19 '13 at 15:53
  • If you use the `Integer.valueOf(int)` method, the `Integer` instances are cached, so you do not create many objects, if that was your concern. – Robin Krahl Nov 19 '13 at 15:53
  • Do you really need an ArrayList, or is any List implementation sufficient? – isnot2bad Nov 19 '13 at 15:58

7 Answers7

7

Something like this:

int[] array = ...;
List<Integer> list = new ArrayList<Integer>(array.length);
for (int i : array) list.add(i);

Note the parameter to the constructor. This will set the initial capacity of the underlying array to what it will need, for best performance.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
2

You have to wrap your ints into an Integer object because you cannot create aList<int> list.

int[] array = new int[1, 2, 3, 4, 5, 6];
List<Integer> list = new ArrayList<Integer>(array.length);
for (int i = 0; i < array.length; i++) {
  list.add(Integer.valueOf(array[i]);
}
Manuel
  • 3,828
  • 6
  • 33
  • 48
0
for(int i : yourArray) {
    yourArrayList.add(i);
}

That should do it

Voidpaw
  • 910
  • 1
  • 5
  • 18
0
int[] old = something;
ArrayList<Integer> list = new ArrayList<Integer>();
for (int x : old){
    list.add(x);
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JoeC
  • 1,850
  • 14
  • 12
0

You can use guava's newArrayList() for this.

Alexander Bezrodniy
  • 8,474
  • 7
  • 22
  • 24
0

If you want to make a List<Integer> out of an int[], and performance is your concern (so all other provided answers here are still 'too slow' for you), you can write a custom List<Integer> implementation that is directly backed by the int[]:

public class IntegerList extends AbstractList<Integer> implements List<Integer> {
    private final int[] data;

    public IntegerList(int[] data) { this.data = data; }

    @Override
    public Integer get(int index) { return data[index]; }

    @Override
    public int size() { return data.length; }
}

// usage
List<Integer> myIntegerList = new IntegerList(myIntArray);

The drawbacks are:

  • The list is not modifiable
  • Changes of the underlying int[] will affect the list instance.
  • Creating Integer objects out of your ints will still happen, it is just deferred until get(int) is called.
isnot2bad
  • 24,105
  • 2
  • 29
  • 50
0

I would use org.apache.commons.lang.ArrayUtils from the Apache Commons Lang library to convert the int[] to an Integer[] and then use Arrays.asList() to back the Array in a List:

import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

int[] array = ...;
List<Integer> list = Arrays.asList(ArrayUtils.toObject(array));
ericbn
  • 10,163
  • 3
  • 47
  • 55