【发布时间】:2012-08-14 19:58:23
【问题描述】:
我正在为 Android 编写应用程序。我使用字符串数组从共享首选项中提取所有玩家名称(必须是字符串数组)。我有一个列表可以从共享偏好中提取所有活跃玩家(必须是一个列表)。我需要检查我的列表以查看它是否包含不在我的播放器数组中的任何播放器并删除它们。我似乎无法弄清楚其中的逻辑。
例如:
列表包含: b C 一种 e
数组包含: 一种 b C d
由于 'e' 存在于 List 但不存在于 Array 中,因此需要从列表中删除 'e'。我知道命令(.contains()、.remove()、for()),我就是搞不懂逻辑。
我的第一次尝试是:
for(int x=0;x<numOfPlayers;x++){
players[x] = getPrefs.getString("player"+Integer.toString(x), " ");
if(activePlayers.size()>0)
if(activePlayers.contains(players[x]))
playersChecked[x] = true;
else{
if(x<activePlayers.size())
activePlayers.remove(x);
}
}
但是如果 player[x] 有一个 activePlayers 没有的项目,这会从 activePlayers 中删除 x,这很好。它需要反过来。
【问题讨论】:
标签: java android arrays string list