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

Time:04-11

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

CodePudding user response:

You should create copies of the original list otherwise the two list will reference the same listTest.
Try using the .mutableListOf() and .listOf() methods:

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

editTransactionList = listTest.toMutableList()
selectedDataEdited = listTest.toList()

CodePudding user response:

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.

CodePudding user response:

Instead of using as MutableList use toMutableList

editTransactionList = listTest.toMutableList()

It will make copy of your list instead of passing the reference

CodePudding user response:

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

  • Related