Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting an object from a given string  RSS feed

 
Max Vandenburg
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following class as a example to a similar problem im having ...

Class Car


Class CreateCarFromList


in the last class i have an arraylist,carNames, with the following value
["Red", "Blue", "Yellow"]

than i do an iteration and create an instance of an object in each iteration and add it to another arraylist, carObject

my problem is what do i need to do, to add to the Car class or perhaps to CreateCarFromList Class.

so that i can say for a

String s = "Red";

give me from carObject list the Car object that has that attribute. (hope this make sense)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
carObject needs to be a HashMap instead of an ArrayList and you use the colour as the key.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!