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