Given the use of List , there is no way to "search" a value without repeating through it ...
For instance...
Cave cave = new Cave(); // Loop adds several Parties to the cave party list cave.parties.add(new Party("FirstParty")); // all anonymously added cave.parties.add(new Party("SecondParty")); cave.parties.add(new Party("ThirdParty")); for (Party p : cave.parties) { if (p.name.equals("SecondParty") { p.index = ...; break; } }
Now it will take some time. If the item you are looking for is at the end of the list, you will need to iterate to the end of the list before finding a match.
It might be better to use some kind of Map ...
So, if we update Cave to look like ...
class Cave { Map<String, Party> parties = new HashMap<String, Party>(25); }
We could do something like ...
Cave cave = new Cave(); // Loop adds several Parties to the cave party list cave.parties.put("FirstParty", new Party("FirstParty")); // all anonymously added cave.parties.put("SecondParty", new Party("SecondParty")); cave.parties.put("ThirdParty", new Party("ThirdParty")); if (cave.parties.containsKey("SecondParty")) { cave.parties.get("SecondParty").index = ... }
Instead of...
Ultimately, all this will depend on what you want to achieve ...
source share