How to Convert an Arraylist Containing Integers to Primitive Int Array

How to convert an ArrayList containing Integers to primitive int array?

You can convert, but I don't think there's anything built in to do it automatically:

public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
for (int i=0; i < ret.length; i++)
{
ret[i] = integers.get(i).intValue();
}
return ret;
}

(Note that this will throw a NullPointerException if either integers or any element within it is null.)

EDIT: As per comments, you may want to use the list iterator to avoid nasty costs with lists such as LinkedList:

public static int[] convertIntegers(List<Integer> integers)
{
int[] ret = new int[integers.size()];
Iterator<Integer> iterator = integers.iterator();
for (int i = 0; i < ret.length; i++)
{
ret[i] = iterator.next().intValue();
}
return ret;
}

How can I convert List Integer to int[] in Java?

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

Convert ArrayList to Int [] array using .toArray()? Or...?

Don't use raw types. Replace ArrayList roomNums = new ArrayList(); with

ArrayList<Integer> roomNums = new ArrayList<>();

Then return it as Integer[]

roomNums.toArray(Integer[]::new);

If you need primitive array, then it can be done with stream:

return roomNums.stream().mapToInt(Integer::valueOf).toArray();

See also How to convert an ArrayList containing Integers to primitive int array?

How to convert int[] into List Integer in Java?

There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method.

int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
intList.add(i);
}

Convert ArrayList of Integer Objects to an int array?

If you want primitive type array, you can use Apache Commons ArrayUtils#toPrimitive method to get primitive array from wrapper array: -

public static int[] toPrimitive(Integer[] array)

You can use it like this: -

int[] arr = ArrayUtils.toPrimitive((Integer[])integerArrayList.toArray());

Or, you can use the 1-arg version of toArray method, that takes an array and returns the array of that type only. That way, you won't have to the typecasting.

List<Integer> myList = new ArrayList<Integer>();
Integer[] wrapperArr = myList.toArray(new Integer[myList.size()]);

// If you want a `primitive` type array
int[] arr = ArrayUtils.toPrimitive(wrapperArr);

However, if you are Ok with the wrapper type array (Which certainly will not trouble you), then you don't need to do that last step.


Also, you can also create a primitive array, without having to create that intermediate Wrapper type array. For that you would have to write it through loop: -

int[] arr = new int[myList.size()];

for(int i = 0; i < myList.size(); i++) {
if (myList.get(i) != null) {
arr[i] = myList.get(i);
}
}

Converting primitive array of int into ArrayList in Java: java.lang.ClassCastException: [I cannot be cast to java.lang.Integer


Solution 1:

In Java 8:

List<Integer> list = Arrays.stream(nums).boxed().collect(Collectors.toCollection(ArrayList::new));

Note: You might need to:

import java.util.stream.Collectors;

Solution 2:

Use for loop:

List<Integer> list = new ArrayList();
for(int n : nums) {
list.add(n);
}

Solution 3:

Declare the original array as Ingeger[] instead of int[]:

Integer[] nums = {0, 1};

Is it possible to convert ArrayList Integer into ArrayList Long ?

Not in a 1 liner.

List<Integer> ints = Arrays.asList(1,2,3,4,5,6,7,8,9);
int nInts = ints.size();
List<Long> longs = new ArrayList<Long>(nInts);
for (int i=0;i<nInts;++i) {
longs.add(ints.get(i).longValue());
}

// Or you can use Lambda expression in Java 8

List<Integer> ints = Arrays.asList(1,2,3,4,5,6,7,8,9);
List<Long> longs = ints.stream()
.mapToLong(Integer::longValue)
.boxed().collect(Collectors.toList());

How do I convert an ArrayList of integers to a double[]?

Best thing to do is to create an array of matching size and then iterate over the entries, casting as you go.

double[] myList = new double[myIntegerValues.size()];
for (int i=0; i<myIntegerValues.size(); i++) {
myList[i] = (double) myIntegerValues.get(i);
}

Note, while I could use an iterator, using indexes makes the one to one relationship clear.



Related Topics



Leave a reply



Submit