Asked  7 Months ago    Answers:  5   Viewed   33 times

How do I convert int[] into List<Integer> in Java?

Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java.

 Answers

58

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);
}
Tuesday, June 1, 2021
 
akohout
answered 7 Months ago
71

To multiply an integer (or byte/short/float/double) with a BigInteger (or BigDecimal), you must convert the native number to BigInteger/BigDecimal first.

// int parameter can be int or Integer
public static BigInteger multiply ( int a, BigInteger b ) {
   return BigInteger.valueOf( a ).multiply( b );
}

// BigInteger <> BigDecimal
public static BigDecimal multiply ( int a, BigDecimal b ) {
   return BigDecimal.valueOf( a ).multiply( b );
}

// same for add, subtract, divide, mod etc.

Note: valueOf is not the same as new, and for different reasons on BigDecimal and BigInteger. In both cases, I recommend valueOf over new.


I see that you added your code, nice. It doesn't work because Integer is mixed with BigDecimal, and also * does not work with BigDecimal. If you compare it with my code, the fix should be obvious:

public BigDecimal methCal ( int quantite, BigDecimal prixUnit ) {
    return BigDecimal.valueOf( quantite ).multiply( prixUnit );
}
Thursday, July 29, 2021
 
superfell
answered 5 Months ago
55

Loop over it yourself.

List<Float> floatList = getItSomehow();
float[] floatArray = new float[floatList.size()];
int i = 0;

for (Float f : floatList) {
    floatArray[i++] = (f != null ? f : Float.NaN); // Or whatever default you want.
}

The nullcheck is mandatory to avoid NullPointerException because a Float (an object) can be null while a float (a primitive) cannot be null at all.

In case you're on Java 8 already and it's no problem to end up with double[] instead of float[], consider Stream#mapToDouble() (no there's no such method as mapToFloat()).

List<Float> floatList = getItSomehow();
double[] doubleArray = floatList.stream()
    .mapToDouble(f -> f != null ? f : Float.NaN) // Or whatever default you want.
    .toArray();
Tuesday, August 3, 2021
 
rob_mccann
answered 4 Months ago
30

To make things work you need to use Integer[] instead of int[].

Argument of asList is of type T... and generic types T can't represent primitive types int, so it will represent most specific Object class, which in this case is array type int[].
That is why Arrays.asList(arrs); will try to return List<int[]> instead of List<int> or even List<Integer>.

Some people expect automatic conversion from int[] to Integer[], but lets nor forget that autoboxing works only for primitive types, but arrays are not primitive types.

Thursday, August 5, 2021
 
apokryfos
answered 4 Months ago
13

Create an additional function called toStars

public String toStars(int number) {
    StringBuilder temp = new StringBuilder();
    for(int i=0;i<number;i++) {
        temp.append("*");
    }
    return temp.toString();
}

Then, alter your print statements as such:

System.out.println("2 : " + toStars(two));
Saturday, August 7, 2021
 
Connor Johnson
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share