How to replace an Integer ArrayList with an int array

Asked 2 years ago, Updated 2 years ago, 82 views

To replace the Integer type ArrayList with an int type array

List<Integer> x =  new ArrayList<Integer>();
int[] n = (int[])x.toArray(int[x.size()]);

I did this, but there is a compilation error. How can I change it?

java arraylist array primitive-types

2022-09-22 22:15

1 Answers

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

You can do it like this.


2022-09-22 22:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.