val testResults = List[TestResult] unable to add

Moderatoren: pmueller, SE - Design and Construction

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

val testResults = List[TestResult] unable to add

Beitrag von AizazZaidee »

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.

labataschö
Windoof-User
Windoof-User
Beiträge: 37
Registriert: 15. Apr 2015 16:43

Re: val testResults = List[TestResult] unable to add

Beitrag von labataschö »

Hi,

it's quite simple - if you already know it :wink:
Don't use val but var.

val (stands for value) is immutable whereas var (stands for variable) is mutable.

Greetings,
labataschö

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: val testResults = List[TestResult] unable to add

Beitrag von AizazZaidee »

No, the actual reason was to use ":" instead of "=" :? :?

Benutzeravatar
AizazZaidee
BASIC-Programmierer
BASIC-Programmierer
Beiträge: 106
Registriert: 20. Apr 2016 22:49

Re: val testResults = List[TestResult] unable to add

Beitrag von AizazZaidee »

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

Benutzeravatar
duyhoan
Neuling
Neuling
Beiträge: 5
Registriert: 2. Mai 2016 01:52

Re: val testResults = List[TestResult] unable to add

Beitrag von duyhoan »

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

Antworten

Zurück zu „Software Engineering - Design and Construction“