Reputation: 341
I have a String eg:
1,TSM,501,SM0156,John Thorne,BCO200,24,30,2,CSM,500,AC1157,Peter Jones,BCO104,24,60,...
The string represents a list of people within a department. The "SM0156" & "AC1157" are their unique identifiers within the department
I would like to loop through the string and create a new 'person object' every time I meet an identifier. The object is then stored in an ArrayList. I believe I could do this with the following code:
deptList.add(new = PersonDetails());
This creates my object but I want to be able to reference it later possibly by the unique identifier! Whilst looping through the original string i have extracted out the identifier in this case "SM0156". I was hoping there was a way to then use this as the reference to the object EG
PersonDetails "SM0156" = new PersonDetails();
deptList.add("SM0156");
Obviously here "SM0156" represents a string but surely I could convert it somehow to use as an reference to my new PersonObject??
Thanks for any help in advance..
Upvotes: 0
Views: 166
Reputation: 21
Try to use HashMaps. HashMaps stores the objects with key/value principal.
http://www.mkyong.com/java/how-to-use-hashmap-tutorial-java/
Upvotes: 0
Reputation: 44449
You do not want to attempt this. Store your details in a collection and depending on the collection you should use it accordingly.
If you choose an ArrayList
, make sure your PersonDetails
class has a field Id
which you can look up (or create a new class that holds an Id
and a PersonDetails
object).
Another solution is creating a Map<String, PersonDetails>
to map the Id
to the person.
Upvotes: 1