I am using companion object to temprorarily save some data.
I might want to change this data and also i want to make sure that original object is not changed when i make changes.
I tried this.
companion object{
var position: Int = 0
}
var copyPosition = positon
copyPosition--
println(copyPosition)
This works perfectly fine and prints -1. Original position
is not changed. (value 0 is not changed.)
However, the same operations with List<MyObject>
is not working.
companion object{
var list: MutableList<MyObject> = "...here objects are aquired..."
}
var tempList: MutableList<MyObject> = list
tempList.removeAt(0)
println(list.size)
Here, if i remove item from tempList
, the original list also loses this item. How can i stop this? How can the changes be only made to tempList
but not original list
?