I have the following code written down.
ArrayList<int []> l = new ArrayList<>();
int [] temp = new int[2];
int n=1;
for(int i=0;i<10;i++)
{
for(int j=0;j<2;j++)
{
temp[j]=n++;
}
l.add(temp);
}
for(int i=0;i<10;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(l.get(i)[j] + " ");
}
System.out.println();
}
The output:
19 20
19 20
19 20
.
.
19 20
I cannot understand why the output is such. i am populating the list with temp array which is changing each time. What am i doing wrong?