Togan J.R
Togan J.R

Reputation: 119

KOTLIN Why when i changed a List it is also accidentally changing another list

Hello i want to know why is my program changing selectedDataEdited List when i only changing editTransactionList ?

var editTransactionList: MutableList<Transaction>? = mutableListOf()
var selectedDataEdited: List<Transaction>? = listOf()

editTransactionList = listTest as MutableList<Transaction>
selectedDataEdited = listTest

var position = 0

println("edit $editTransactionList")
println("select $selectedDataEdited")

editTransactionList.get(position).apply {
    amount = 2000
    name = "K"
}
println("edit $editTransactionList")
println("select $selectedDataEdited")

editTransactionList.get(position).apply {
    amount = 3000
    name = "Z"
}
println("edit $editTransactionList")
println("select $selectedDataEdited")

the output is

edit [Transaction(amount=1000, name=T, test=1)]
select [Transaction(amount=1000, name=T, test=1)]
edit [Transaction(amount=2000, name=K, test=1)]
select [Transaction(amount=2000, name=K, test=1)]
edit [Transaction(amount=3000, name=Z, test=1)]
select [Transaction(amount=3000, name=Z, test=1)]

Upvotes: 1

Views: 686

Answers (4)

Tolgahan Albayram
Tolgahan Albayram

Reputation: 31

If selectedDataEdited = listTest.map { it.copy() } is not working, try below code:

 listedTest.forEach {
        selectedData.add(Transaction(amount = it.amount, name = it.name, test = it.test))
    }

Upvotes: 1

mmdreza baqalpour
mmdreza baqalpour

Reputation: 1403

Instead of using as MutableList use toMutableList:

editTransactionList = listTest.toMutableList()

It will make a copy of your list instead of passing a reference to the same list.

Upvotes: 0

Ivo
Ivo

Reputation: 23154

Variables are basically references. When you store an object in a variable you actually say "when using this variable please refer to this object". So if you "store" the same object into 2 different variables, each of them still refers to that same object. Getting the object using the first variable, making changes to it, and then accessing the second variable, will still get you that changed object.

You will need to copy the list to prevent the unwanted behavior. Keep in mind though that you would probably need a deep copy. Simply calling toList() on it for example only makes a shallow copy, which means that even though it will be a different list, the objects inside it will still refer to the original.

It's hard to tell what would work without knowing what Transaction looks like. If Transaction is a data class then selectedDataEdited = listTest.map { it.copy() } might work. See this example https://pl.kotl.in/Q_o8pYXVs

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83527

KOTLIN Why when i changed a List it is also accidentally changing another list

Because you don't have "another" list. You only have one list.

When you do selectedDataEdited = listTest, you assign a second reference to the same list. If you want two separate lists, you must create them, possibly by cloning the original list.

Upvotes: 0

Related Questions