I just found a strange behavior with take() method in a String.
Here is my code:
val ji = Array("134","231","2321")
var t = ji
var i = ji(1).take(2)
i = i + 8
t(1)= i
println(ji.mkString(",")) //134,238,2321
println(t.mkString(",")) //134,238,2321
I expected that ji kept its value but it seems affected as well as t. I would like to create a new ji then change into t without touching ji. However, I don't want to make ji redundant.
val ji = Array("134","231","2321")- Ok,jiis an array of strings.var t = ji-- Don't need this. AVOID usingvarsvar i = ji(1).take(2)-- Don't need this eitheri = i + 8- AVOID mutability. Also,iis aStringand8is an integer. DON'T do this.The two lines above should be rewritten as
val i = ji(1).take(2) + "8"t(1)= i-- You are changing the array element here (as written,tandjiare the same array). DON'T do this.Bottom line:
This does what you want:
jiandtare two arrays, that differ in their second element.