【发布时间】:2012-08-22 02:43:38
【问题描述】:
只是做了一个小安卓应用,我需要保存一个数据集合,不确定是否应该使用对象、数组、listarray、hashmap等。
它需要能够存储字符串和整数(例如名称、分数、类别、圈数)。
它需要能够存储可变数量的团队。
目前我尝试将其存储为对象时遇到问题,但它不会让我增加 score int 值。
我应该在这个实例中使用哪个集合,在其他实例中我应该在什么时候使用每个集合?
编辑
好的,这样做了,但是当我尝试访问/增加它时,我不断收到 NullPointerException。我只能假设它无法从 upScore 正确访问 team var
所以我的onCreate是这样的
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"));
}
然后当按下按钮时调用 upScore。致命异常在这一行
public void upScore(int team_key) {
teams.get(0).score++;
}
这是我的团队对象
class Team {
public String name;
public int score;
public String category;
public int turns;
public Team (String teamName) {
name = teamName;
}
}
【问题讨论】:
-
存储在 Team、Match、Play 类的对象中?
-
我建议您先阅读一下,然后才能选择合适的集合。当您了解它们各自的优点时,您就会明白哪一个最适合您。
标签: java android arrays collections multidimensional-array