assassinsweed1 assassinsweed1 - 1 month ago 12
Java Question

Converting ArrayList<Integer> into int[] won't work in Java

I have been developing a program where I have to convert an ArrayList into an int[]. I do not get any syntax errors when I run the code. However, when I print the int[] to see if it does work, it prints out a random string which is "[I@2a139a55" How can I fix this?

I cannot use an int[] from the start. This program HAS to convert an ArrayList into int[].

ArrayList<Integer> student_id = new ArrayList<Integer>();
student_id.add(6666);
student_id.add(7888);

int[] student_id_array = new int[student_id.size()];
for (int i=0, len = student_id.size(); i < len; i ++){
student_id_array[i] = student_id.get(i);
}
System.out.println(student_id_array);

Answer

You are printing out the reference to the array. Use Arrays.toString(int[]).