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
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.
© 2024 OneMinuteCode. All rights reserved.