Because Option 2 creates a new List
reference, and then creates an n
element array from the List
(option 1 perfectly sizes the output array). However, first you need to fix the off by one bug. Use <
(not <=
). Like,
String[] out = new String[n];
for(int i = 0; i < n; i++) {
out[i] = in.get(i);
}