Re: int[] array
Hendrik Maryns wrote:
O.L. schreef:
I need int[] arrays in some functions.
Use ArrayList. Why do you need the int[]? What can you do with it
that you cannot do with ArrayList?
If you really want the int, you can do myList.toArray(new
Integer[myList.size()])
This gives you an Integer[]. Autoboxing will do the rest.
I suspect autoboxing won't convert object arrays to native arrays.
Eclipse gives me a compiler error: "The method emit(int[]) in the type
IntArrayList is not applicable for the arguments (Integer[])"
-------------------------------------8<----------------------------
import java.util.ArrayList;
public class IntArrayList {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(1);
list.add(5);
// 1. Use Integer[]
Integer[] intArray = list.toArray(new Integer[0]);
emit(intArray); // ******** compiler error ********
// 2. use int[]
int[] ints = new int[list.size()];
for (int i = 0; i < ints.length; i++)
ints[i] = list.get(i);
emit(ints);
}
static void emit(int[] intz) {
for (int i = 0; i< intz.length; i++)
System.out.println(intz[i]);
}
}
-------------------------------------8<----------------------------
Am I missing something?