英文:
Having trouble copying an ArrayList
问题
以下是翻译好的部分:
这可能是一个有点愚蠢的问题,但我弄不清楚这个!我正在开发一个迷你游戏,其中一个玩家会被随机选择为终结者,而其余的都是弱者。我有一个玩家列表(ArrayList),我可以让它选择终结者,然后我需要将玩家复制到弱者列表,但跳过终结者。我正在使用的代码根本没有复制任何东西到弱者列表。以下是我正在使用的代码,请有更多经验的人帮我看看:
static void createWeaklingList() {
for (int i = 0; i < players.size(); i++) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("其他玩家是:" + weaklings + "。");
}
英文:
This might be a bit of a stupid question, but I can't figure this out! I'm working on a minigame that involves one player being randomly selected as the terminator, and all the rest as weaklings. I have an ArrayList of players, and I can get it to choose the terminator, and then I need it to copy players into weaklings, but skip terminator. The code I'm using doesn't copy anything at all into weaklings. Here's the code I'm using, could someone a bit more experienced help me out please:
for (int i = 0; i < players.size(); i++ ) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("The other players are: " + weaklings + ".");
}
</details>
# 答案1
**得分**: 2
你再次添加到`players`中:`players.add`,而不是`weaklings.add`。
<details>
<summary>英文:</summary>
You add to the `players` again: `players.add`, instead of `weaklings.add`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论