I am trying to use ArrayList<ArrayList<String>> to parse some fragment from web page but something goes wrong within my code:
ArrayList<ArrayList<String>> x = new ArrayList<ArrayList<String>>();
ArrayList<String> s = new ArrayList<String>();
int z, y, w = 0;
for(z=0; z<3;z++)
{
s.clear();
for(y=0;y<4;y++)
{
s.add(""+w++);
}
x.add(s);
}
for(ArrayList<String> tr : x)
{
for( String t : tr)
{
System.out.println(t);
}
System.out.println("-----------------");
}
The output is:
8
9
10
11
8
9
10
11
8
9
10
11
instead of (my expected output):
0
1
2
3
4
5
6
7
8
9
10
11
Can somebody explain to me why is this happening? Thanks.
sis a reference to an object. When you add a copy of this reference it just adds that reference, not a copy of the object referenced.