I'm struggling with a method dynamically returning an
ArrayList<Player>
Here's what I have:
Fantasy class
public class Fantasy {
Squad team;
ArrayList<Player> substitutes = team.getList(substitutes);
}
Squad class
public class Squad {
ArrayList<Player> substitutes;
// Some code adding stuff into the ArrayList
public ArrayList<Player> getList(String list) {
return ArrayList<Player> list; << PROBLEM
}
}
I want to have a method getList() through which I can pass a name of an ArrayList, which will check if an ArratList with the same name exists, and if yes, return it back as an ArrayList.
The problem is that I have no idea how to check if there's an ArrayList named the same as the String list I'm passing.
POSSIBLE SOLUTION:
Map<String, ArrayList<Player>> arrayLists = new HashMap<String, ArrayList<Player>>(){{
put("goalKeepers", goalKeepers);
put("defenders", defenders);
put("midFielders", midFielders);
put("strikers", strikers);
put("substitutes", substitutes);
}};
and
public ArrayList<Player> getList(String list) {
return arrayLists.get(list);
}
BUT, when I call:
ArrayList test = getList("substitute");
or whenever I use getList(); I get the following error:
Cannot make a static reference to the non-static method getList(String) from the type Squad
getSubstitutes()method that returns the list:return substitutes;?Squadclass, so you only have the one possible list to return. Also your statement "check if such method exists" makes no sense in relation to the rest of your question. Perhaps you should look up what the term "method" means in the context of a Java application. In general your question makes no sense as currently stated.Map<string,List <Player>>If you need to pair strings to collections of players