C#list.remove正在做一些奇怪的事情 [英] C# list.remove is doing weird stuff

查看:86
本文介绍了C#list.remove正在做一些奇怪的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在c#中制作一个基于文本的饥饿游戏模拟器。

在我的代码中,我有一个包含所有玩家和他们的统计数据的列表。

这个列表是由一个名为Player的类,其中包含变量名称,杀死和一个活着的bool来检查或者玩家还活着。



在我的代码中我需要随机选择一个活着的球员。这个问题是因为在游戏的后期阶段,因为很多人活着而且随机发生器总是挑选死了的人并且我无法使用那些随机玩家因此花了太长时间才得到随机玩家。



所以我决定添加一个名为AlivePlayers的第二个名单。它与此列表中的普通玩家列表相同,我可以在玩家死亡时从玩家中删除玩家。我不能在玩家列表中这样做因为我需要保持玩家的统计数据。



所以解决了这个问题我开始编写代码来实际删除玩家从列表中。这就是奇怪的事情。



我首先开始编写代码以从alivePlayers列表中删除玩家。我启动了我的程序,并在监视窗口中跟踪列表的值。然后,我看到我运行列表的删除方法的那一刻,项目从alivePlayers列表中删除,但也来自播放器列表。首先,我认为问题是我的代码中的其他地方所以我评论删除部分并再次尝试。但是什么都没有删除gots,所以为什么删除方法从2个列表中移除?



这里是代码的一部分:

hello, im making a textbased hungergames simulator in c#.
In my code i have a list with all players and their stats.
this list is made out of a class called Player with variables i nit like name,kills and an alive bool to check or the player is alive.

somtimes in my code i need to pick a random player that is alive. the problem wit this was that it taked too long to get a random player becouse at the late stages of the game njot manyy people where alive and the random generator always picked people who are dead and i cant use those.

so i decided to add a second list with players called AlivePlayers. it is the same as the normal players list onlt in this list i can remove the players from the lsit when they are dead. i cant do this in the players list becouse i need to keep the stats from the players.

so with that problem solved i started writing the code to actual remove the player from the list. and thats where things gor weird.

i first started with writing the code to remove the players fromt the alivePlayers list. i started my program and followed the values of the lists in th watch window. i then saw that the moment i runned the remove methode of the list that the items get removed from the alivePlayers list but ALSO from the players list. first i tought the problems was somwhere else in my code so i commented the remove part out and tried again. but know nothing gots removed so why does the remove methode removes from 2 lists ?

here is the part of the code that does it:

if(action.Element("Action").Element("lethalto").Value != "none")
     {
         string rawString = action.Element("Action").Element("lethalto").Value;
         string[] playersToKill = rawString.Split('-');

         foreach(string player in playersToKill)
         {
             string playerIdString = player.Replace('p', ' ');
             int playerId = Int32.Parse(playerIdString);
             SurvivalGames.alivePlayers.Remove(affectedPlayers[playerId - 1]);

         }
     }



不要专注于rawString和playersToKill。编写我的代码,以便可以使用xml进行自定义。这些变量仅用于获取需要删除的玩家的索引。这2个工作正常,我已经测试过了。



affectedPlayers是受影响的玩家列表。在这种情况下,我想从alivePlayers列表中删除受影响的玩家。 playerId是我们需要删除的受影响玩家列表中玩家的索引。我减去1,这样我就不会出错了。



所以有人知道为什么remove()方法从两个列表中删除而不仅仅是从alivePlayers中删除?



ps:抱歉我的英语不好,这不是我的主要语言。如果你不明白,可以随时提问。



我尝试过的事情:



搜索谷歌但我没有发现任何与我的问题相关的内容。


dont focus at rawString and playersToKill. my code is written so that it is customizable with xml. those variables are only for getting the indexes of the players that need to be removed. those 2 work fine i already tested them.

affectedPlayers is a list of players that are affected. in this case i want to remove the affected players form the alivePlayers list. the playerId is the index of the player from the affected players list that we need to remove. i substract 1 so that i do not get an out of bounds error.

so someone knows why the remove() methode deleted from both lists and not only from alivePlayers ?

ps: sorry for my bad english it is not my primary language. and feel free to ask questions if you do not understand something.

What I have tried:

searching on google but i did not found anything related to my problem.

推荐答案

根据你的描述,你有两个指向同一个列表< T> 的实例。无论您使用哪个变量来访问该实例,对该实例所做的任何更改都是可见的。

Based on your description, you have two variables pointing to the same instance of List<T>. Any changes made to that instance will obviously be visible whichever variable you use to access it.
List<int> x = new List<int>();
List<int> y = x; // Points to the same instance.

x.Add(42);

Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 1

y.Remove(42);

Console.WriteLine(x.Length); // Output: 0
Console.WriteLine(y.Length); // Output: 0



你是什么need是 List< T> 的单独实例。

注意:如果一个项目需要在两个列表中,那么您需要将它添加到两个列表中。


What you need is a separate instance of List<T>.
NB: If an item needs to be in both lists, then you will need to add it to both lists.

List<int> x = new List<int>();
List<int> y = new List<int>(); // Two separate instances.

x.Add(42);
Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 0

y.Add(42);
Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 1

y.Remove(42);
Console.WriteLine(x.Length); // Output: 1
Console.WriteLine(y.Length); // Output: 0


这篇关于C#list.remove正在做一些奇怪的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆