Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 533 characters in body
Source Link
Pshemo
  • 124.6k
  • 26
  • 194
  • 280

Before reading this answer you may want to familiarize yourself with What is the difference between a variable, object, and reference?


This is what you have now

which creates two separate lists and stores reference to them in variables outer and inner.

which in fact copy valueHere value of inner reference which means they pointvariable (so reference to same list from[100, 200]) is placed in innerouter list two times. This means that outer "points to" [100, 200] list two times

//note: `reference1` == `reference2` (like 42 == 42) since they point to same object
outer -> [ reference1, reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner +-> [100, 200]

whichThis means that if you change state of list held by inner[100, 200] you will be able to see these changes using reference1outer.get(0) andor reference2outer.get(1) or inner, since all of them are reference to same list. Same

So if you change this list via other references, so when youwe use

the outer.get(0) returns reference to same list which you can access also viaas inner orand getadd(1300) and addingadds new element so nowto that list. Which means that after it new situation lookswill look like

What you actually need is to create separate list so reference1 and reference2 will point to two separate lists. So you need somethingSomething like

which will be later organized tointo

List<List<Integer>> outer = new ArrayList<List<Integer>>();
List<Integer> inner1 = new ArrayList<Integer>();
List<Integer> inner2 = new ArrayList<Integer>();
inner1.add(100);
inner1.add(200);
inner2.add(100);
inner2.add(200);
outer.add(inner1);
outer.add(inner2);
outer.get(0).add(300);
System.out.println(outer);
//Output: [[100, 200, 300], [100, 200]]

This is what you have now

which in fact copy value of inner reference which means they point to same list from inner

outer -> [ reference1, reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner +-> [100, 200]

which means that if you change state of list held by inner you will be able to see these changes using reference1 and reference2. Same if you change this list via other references, so when you use

get(0) returns list which you can access also via inner or get(1) and adding new element so now situation looks like

What you actually need is to create separate list so reference1 and reference2 will point to two separate lists. So you need something like

which will be later organized to

List<List<Integer>> outer = new ArrayList<List<Integer>>();
List<Integer> inner1 = new ArrayList<Integer>();
List<Integer> inner2 = new ArrayList<Integer>();
inner1.add(100);
inner1.add(200);
inner2.add(100);
inner2.add(200);
outer.add(inner1);
outer.add(inner2);
outer.get(0).add(300);
System.out.println(outer);

Before reading this answer you may want to familiarize yourself with What is the difference between a variable, object, and reference?


This is what you have now

which creates two separate lists and stores reference to them in variables outer and inner.

Here value of inner variable (so reference to list [100, 200]) is placed in outer list two times. This means that outer "points to" [100, 200] list two times

//note: `reference1` == `reference2` (like 42 == 42) since they point to same object
outer -> [ reference1, reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner +-> [100, 200]

This means that if you change state of list [100, 200] you will be able to see these changes using outer.get(0) or outer.get(1) or inner, since all of them are reference to same list.

So if we use

the outer.get(0) returns reference to same list as inner and add(300) adds new element to that list. Which means that after it new situation will look like

What you actually need is to create separate list so reference1 and reference2 will point to two separate lists. Something like

which will be later organized into

List<List<Integer>> outer = new ArrayList<List<Integer>>();
List<Integer> inner1 = new ArrayList<Integer>();
List<Integer> inner2 = new ArrayList<Integer>();
inner1.add(100);
inner1.add(200);
inner2.add(100);
inner2.add(200);
outer.add(inner1);
outer.add(inner2);
outer.get(0).add(300);
System.out.println(outer);
//Output: [[100, 200, 300], [100, 200]]
added 2 characters in body
Source Link
Pshemo
  • 124.6k
  • 26
  • 194
  • 280

which in fact copy value of inner reference which means they holdpoint to same list as one from inner

outer -> [ reference1 , reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner +-> [100, 200]
[[100, 200, 300], [100, 200, 300]]. 
 ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
 from get(0) from get(21)

What you actually need is to create separate list so reference1reference1 and reference2reference2 will point to two separate lists. So you need something like

which in fact copy value of inner reference which means they hold same list as one from inner

outer -> [ reference1 , reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner -> [100, 200]
[[100, 200, 300], [100, 200, 300]]. 
 ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
 from get(0) from get(2)

What you actually need is to create separate list so reference1 and reference2 will point to two separate lists. So you need something like

which in fact copy value of inner reference which means they point to same list from inner

outer -> [ reference1 , reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner +-> [100, 200]
[[100, 200, 300], [100, 200, 300]]. 
 ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
 from get(0) from get(1)

What you actually need is to create separate list so reference1 and reference2 will point to two separate lists. So you need something like

Source Link
Pshemo
  • 124.6k
  • 26
  • 194
  • 280

This is what you have now

ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> inner = new ArrayList<Integer>(); 

will create

outer -> []
inner -> []

After

inner.add(100); 
inner.add(200);

your situation looks like

outer -> []
inner -> [100, 200]

Here comes confusing part

outer.add(inner);
outer.add(inner);

which in fact copy value of inner reference which means they hold same list as one from inner

outer -> [ reference1 , reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner -> [100, 200]

which means that if you change state of list held by inner you will be able to see these changes using reference1 and reference2. Same if you change this list via other references, so when you use

outer.get(0).add(300);

get(0) returns list which you can access also via inner or get(1) and adding new element so now situation looks like

outer -> [ reference1 , reference2 ]
 | |
 +-------+ |
 +---------------------+
 ↓
inner -> [100, 200, 300]

That is why when you print outer you are seeing

[[100, 200, 300], [100, 200, 300]]. 
 ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
 from get(0) from get(2)

What you actually need is to create separate list so reference1 and reference2 will point to two separate lists. So you need something like

outer -> []
inner1 -> [100, 200]
inner2 -> [100, 200]

which will be later organized to

outer -> [ reference1 , reference2 ]
 | |
 +------+ |
 ↓ |
inner1 -> [100, 200] |
 |
 +--------------------+
 ↓
inner2 -> [100, 200]

You can do it this way

List<List<Integer>> outer = new ArrayList<List<Integer>>();
List<Integer> inner1 = new ArrayList<Integer>();
List<Integer> inner2 = new ArrayList<Integer>();
inner1.add(100);
inner1.add(200);
inner2.add(100);
inner2.add(200);
outer.add(inner1);
outer.add(inner2);
outer.get(0).add(300);
System.out.println(outer);
lang-java

AltStyle によって変換されたページ (->オリジナル) /