TL,DR
I have a Kotlin code where elements are added to arraylist but the list is not storing the new value although the return for the function shows the changed list.
Example problem:
/* someList = ["A", "ab"] */
val x = someList.plus("Abc")
Example output:
someList = ["A", "ab"] , x = ["A", "ab", "Abc"]
Full Problem
My code:
class FriendClass{
/* others */
@JsonProperty("messages")
var messages: List<String>? = ArrayList()
}
class DataClass{
/* singleton */
/* others */
@JsonProperty("friend_detail_list")
var friendDetailList: List<FriendClass> = ArrayList()
}
class Adapter{
val a = DataClass.getInstance().friendDetailList[0].messages?.plusElement("Hello")
val b = DataClass.getInstance().friendDetailList[0].messages
val c = DataClass.getInstance().friendDetailList[0].messages?.toMutableList().add("Hello")
val d = DataClass.getInstance().friendDetailList[0].messages
/* printing a, b, c, d */
}
My output:
messages already had ["a", "bcd", "xfd"]
Now after printing the results are:
a = ["a", "bcd", "xfd", "Hello"]
b = ["a", "bcd", "xfd"]
a = true
b = ["a", "bcd", "xfd"]
How dow I make the original messages update it's values in the FriendClass?