Skip to main content
added 109 characters in body
Source Link
Sergio
  • 31.1k
  • 22
  • 143
  • 159

When you create an object and assign a reference to that object to List_A, a memory is allocated for that object and it has some memory address, say @eae072e. List_A is a reference to that object. When you create another lista variable List_B and assign to it the reference to List_A, they both refer to the same address in memory @eae072e. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.

To avoid it new instance of List_B should be created:

var List_B: MutableList<String> = ArrayList<String>() List_B.addAll(List_A) 

And then you can clear the list List_A and it not be reflected in List_B:

List_A.clear() // List_A is empty, List_B will contain items 

When you create List_A it has some memory address, say @eae072e . When you create another list List_B and assign to it the reference to List_A, they both refer to the same address in memory @eae072e. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.

To avoid it new instance of List_B should be created:

var List_B: MutableList<String> = ArrayList<String>() List_B.addAll(List_A) 

And then you can clear the list List_A and it not be reflected in List_B:

List_A.clear() // List_A is empty, List_B will contain items 

When you create an object and assign a reference to that object to List_A, a memory is allocated for that object and it has some address, say @eae072e. List_A is a reference to that object. When you create a variable List_B and assign to it List_A, they both refer to the same address in memory @eae072e. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.

To avoid it new instance of List_B should be created:

var List_B: MutableList<String> = ArrayList<String>() List_B.addAll(List_A) 

And then you can clear the list List_A and it not be reflected in List_B:

List_A.clear() // List_A is empty, List_B will contain items 
added 316 characters in body
Source Link
Sergio
  • 31.1k
  • 22
  • 143
  • 159

When you create List_A it has some memory address, say 0x574a87@eae072e . When you create another list List_B and assign to it the reference to List_A, they both refer to the same address in memory 0x574a87@eae072e. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.

To avoid it new instance of List_B should be created:

var List_B: MutableList<String> = ArrayList<String>() List_B.addAll(List_A) 

And then you can clear the list List_A and it not be reflected in List_B:

List_A.clear() // List_A is empty, List_B will contain items 

When you create List_A it has some memory address, say 0x574a87 . When you create another list List_B and assign to it the reference to List_A, they both refer to the same address in memory 0x574a87. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.

When you create List_A it has some memory address, say @eae072e . When you create another list List_B and assign to it the reference to List_A, they both refer to the same address in memory @eae072e. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.

To avoid it new instance of List_B should be created:

var List_B: MutableList<String> = ArrayList<String>() List_B.addAll(List_A) 

And then you can clear the list List_A and it not be reflected in List_B:

List_A.clear() // List_A is empty, List_B will contain items 
Source Link
Sergio
  • 31.1k
  • 22
  • 143
  • 159

When you create List_A it has some memory address, say 0x574a87 . When you create another list List_B and assign to it the reference to List_A, they both refer to the same address in memory 0x574a87. So when you use one of them to manipulate with data, this manipulation will be reflected in both of them - in List_A and List_B.