Mutable array get overridden when get asserted to another variable

January 12, 2019, at 9:10 PM

I have A simple case where I Have "List A" with 5 values and another "List B" acts As TempList holding "List A" values now when I call "list_A.clear()"
"list_B" also get Cleared why?

val List_A: MutableList<String> = ArrayList<String>()

val  List_B = List_A
List_A-----Size--------> 0
List_B-----Size--------> 0

Please Note that it works as expected when I define "List_B" as

var List_B: MutableList<String> = ArrayList<String>()
List_A-----Size--------> 0
List_B-----Size--------> 5

Does kotlin passes List_A variable Referance to List_B ?

Answer 1

Does kotlin passes List_A variable Referance to List_B ?

Exactly. Both List_A and List_B refer to the same address in memory. If you want to make a copy you need to create a new ArrayList and add all elements into it with addAll. After that you may clear List_A.


val listA: MutableList<String> = mutableListOf("index_0", "index_1", "index_2")
val listB: MutableList<String> = mutableListOf()

Although it'll work fine with String in your case, you must understand that new list contains same objects (reference to the same objects), not a copy of them. So if you don't clear listA and change an element of it, that element will also be changed in listB accordingly.

Answer 2

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>()

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

// List_A is empty, List_B will contain items
Answer 3

listA and listB both hold the same reference the time you call clear().

What you need to do is to make a copy of listA by calling toMutableList().

val listA = ArrayList<String>()
// ...
val listB = listA.toMutableList()
// listB will be unchangend


  • You should stick to Kotlin's naming conventions, so your variables should be in camel-case without underscore.
  • The type of listA can be inferred, so there is no need to specifiy the type explicitely.
Why does my ForegroundService end when few hours pass?

Why does my ForegroundService end when few hours pass?

I have a ForegroundService which it is sending locations and it is running well but when it is pass more or less 12 hours the service stopsI have the same issue when I put battery saving

Can&#39;t run expo using LAN

Can't run expo using LAN

It works using tunnel, but when I run it on LAN it throws a weird error

Web interface to control android layout [on hold]

Web interface to control android layout [on hold]

I wanted to make a web interface (could be a flash one) that will have some widgets inside and a canvas of selected resolution, I could drag and drop windows onto the canvas and move the object to specific x y coordinatesAfter you finish the layout you save and the Android...

Order table by date stored in text column

Order table by date stored in text column

I have and app and storing some data on SQLiteAnd i have a column named date and storing string data format like: