Just making a little android app, and I need to hold a collection of data, and not sure if I should use an object, array, listarray, hashmap, etc.
It needs to be able to store strings and integers (eg tamename, score, category, number of turns).
It needs to be able to store a variable number of teams.
Currently having issues where I try to store it as an object, but it wont lt me increment the score int value.
Which collection should I use for this instance, and when should I use each one in other instances?
EDIT
Ok so did that, but I keep getting NullPointerException when i try to access / increment it. I can only assume it cant access the teams var properly from upScore
So my onCreate is like this
public ArrayList<Team> teams;
public int currentTeam;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
currentTeam = 0;
List<Team> teams = new ArrayList<Team>();
teams.add(new Team("Team 1"));
teams.add(new Team("Team 2"));
}
then upScore gets called when a button is pressed. the fatal exception is on this line
public void upScore(int team_key) {
teams.get(0).score++;
}
And here is my Team object
class Team {
public String name;
public int score;
public String category;
public int turns;
public Team (String teamName) {
name = teamName;
}
}
Create your own class that represents objects which can hold the data you need, something like:
Then use any data structure you want, for example, an arraylist:
To add elements to the list:
To increment the score of the first team in the list:
and so on…