遇到问题复制一个ArrayList

huangapple go评论56阅读模式
英文:

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 &lt; players.size(); i++ ) {
				
				if (!players.get(i).contentEquals(terminator)) {
					
					players.add(players.get(i));
					
				}
				
			}
			
			System.out.print(&quot;The other players are: &quot; + weaklings + &quot;.&quot;);
				
			}

</details>


# 答案1
**得分**: 2

你再次添加到`players`中:`players.add`,而不是`weaklings.add`。

<details>
<summary>英文:</summary>

You add to the `players` again: `players.add`, instead of `weaklings.add`.

</details>



huangapple
  • 本文由 发表于 2020年8月5日 19:41:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63264409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定