I have a very simple Java class:
public class Party {
private List<String> hosts = new ArrayList<String>();
private String headHost = null;
public void addHost(String name) {
hosts.add(name);
headHost = name;
}
}
But it seems a bit silly to maintain a String keeping track of the list’s pointer…
1. Is there a better way for keeping track of who is the “head host”?
It won’t always be the last person added, either:
public void setHost(int i) {
headHost = hosts.get(i); // potentially throws an exception based on i
}
public void setHost(String name) {
headHost = name;
}
2. What’s the best way to change the current “head host”? Both of these methods could potentially take incorrect parameters.
The current version is just fine. If you are concerned about exceptions, you can apply some defensive programming to the code: