Hi,
I have spent like 2 hours just finding how to add and element to the list. I don't know if I am really poor at Googling or it's just Scala :p
val testResults = List[TestResult]
How can I add elements into it and how can I check contains? I have searched a lot and everywhere there are examples with integer or String that works but I am unable to find out how to do it with custom object? must be pretty simple !!!
Thanks.
val testResults = List[TestResult] unable to add
Moderatoren: pmueller, SE - Design and Construction
- AizazZaidee
- BASIC-Programmierer
- Beiträge: 106
- Registriert: 20. Apr 2016 22:49
-
- Windoof-User
- Beiträge: 37
- Registriert: 15. Apr 2015 16:43
Re: val testResults = List[TestResult] unable to add
Hi,
it's quite simple - if you already know it
Don't use val but var.
val (stands for value) is immutable whereas var (stands for variable) is mutable.
Greetings,
labataschö
it's quite simple - if you already know it

Don't use val but var.
val (stands for value) is immutable whereas var (stands for variable) is mutable.
Greetings,
labataschö
- AizazZaidee
- BASIC-Programmierer
- Beiträge: 106
- Registriert: 20. Apr 2016 22:49
Re: val testResults = List[TestResult] unable to add
No, the actual reason was to use ":" instead of "="



- AizazZaidee
- BASIC-Programmierer
- Beiträge: 106
- Registriert: 20. Apr 2016 22:49
Re: val testResults = List[TestResult] unable to add
for that you can have mutable list buffer, if you want mutable list, I don't know why in assignment it is stressed to have an immutable list
Re: val testResults = List[TestResult] unable to add
Hi,
You can still use the List with no problem. I think the reason why they suggest using Immutable List is that the element (TestResult object) in the list isn't expected to be modified. Back to your question, you can use :: operator to append new element to the List as metioned in the Scala documentation
And also define the List field using var keyword, otherwise the List cannot be re-assigned.
Cheers,
Hoan
You can still use the List with no problem. I think the reason why they suggest using Immutable List is that the element (TestResult object) in the list isn't expected to be modified. Back to your question, you can use :: operator to append new element to the List as metioned in the Scala documentation

Cheers,
Hoan