Whenever I've been trying to write an array to any type of a list I get the same output and I don't seem to understand where I'm doing wrong. For some reason, every array instance in the Arraylist equals to last cycle of the for loop.
Here's the example code
public class Test {
public static void main(String[] args) {
ArrayList<String[]> al = new ArrayList<>();
String[] a = new String[2];
for (int i = 0; i < 4; i++) {
a[0] = "boo";
a[1] = String.valueOf(i);
al.add(a);
}
}
}
With the output
[[boo, 3], [boo, 3], [boo, 3], [boo, 3]]