I know i can concat two lists a & b to result c like this in scala. But how can i do same thing if i do not want extra variable c and hold result in variable a.
val a = List(1,2,3)
val b = List(4,5,6)
val c = a ::: b (I do not want this extra variable c and want to hold result in val a only)
Basically appending List b to List a:
Something like [ a = a ::: b]
This is giving me error: Reassignment to val.
A val
is final. Once a value has been assigned to it ( List(1,2,3)
), a new value cannot be reassigned to it. Use a var
Use var
instead of val
. val
can't be re-assigned.
You can treat it as a:::b
anywhere in your code . BUT note that even if you define a new val c = a:::b
it does not allocate more memory size (as the size of a
and b
). it uses data sharing of a
and b
so it good practice to create a new val if it makes your code more readable (or makes your life easier) otherwise just use a:::b . in general it is a better practice than using a 'var'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.