参考清单问题. [英] Reference to List question .

查看:69
本文介绍了参考清单问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然是因为我对引用类型缺乏了解.
当我尝试创建随机列表时,原始列表是
的副本 随机的.我敢肯定我会很愚蠢的答案,但这是代码-

So apparently because of my lack of understanding of reference types.
when I try to create a random list the original list is a duplicate of the
random one. I''m sure I will feel stupid for the answer but here is the code -

class TwoLists
{
    List<Player> _players;
    public List<Player> Players
    {
        get { return _players; }
    }
    //constructor
    public TwoLists()
    {
        _players = new List<Player>();
        CreateLists();
    }
    private void CreateLists()
    {
        for (int i = 0; i < 10; i++)
        {
            Player p = new Player("Player " + i.ToString());
            _players.Add(p);
        }
        CreateRandomList();
    }
    private void CreateRandomList()
    {
        List<Player> tempList = new List<Player>();
        tempList = _players; //  <<<<<<<<<
        Player tempPlayer = new Player();
        Random r = new Random();
        int pCount = _players.Count - 1;
        int rNum = 0;
        for (int i = 0; i <= pCount; i++)
        {
            for (int j = 0; j < 100; j++)
            {
                rNum = r.Next(0, pCount);
                tempPlayer = tempList[i];
                tempList[i] = tempList[rNum];
                tempList[rNum] = tempPlayer;
            }
        }
                // I DID NOT TELL _players TO = templist .
    }
}


因此,现在当我访问Player时,即使我没有将其随机化,它也会被随机化.
所以tempList是_players的=.然后我使用临时列表和_players
复制它.所以我的问题是如何保持_players完整.
以及这里发生了什么.显然,我应该现在就明白这一点,
好吧,玛比您的回答会让我明白我肯定应该了解的内容.
非常感谢您的宝贵时间.


So Now when I access Players it is randomized even though I have not randomized it.
so tempList is = to _players. then I work with templist and _players
duplicates it. so My question is how do I leave _players intact.
and what is going on here. obviously I should understand this by now and
so mabey Your answer will make me see what I surely should have aready understood.
and I appreciate your time. also maybey good for other beginners.

推荐答案

tempList = _players对tempplayer所做的任何操作对_player所做的任何操作,它们都引用同一对象.

您需要创建一个新列表,然后复制每个元素.

最好的问候
Espen Harlinn
tempList = _players anything you do to tempList you do to _players, they are both referencing the same object.

You need to create a new list, then make a copy of each element.

Best regards
Espen Harlinn


嗯,实际上,您做到了.
这不会太容易,没有图片并且时不时地进行快速检查,以查看您的眼睛是否闪闪发亮,但是在这里...

您有一个变量"players",它引用Player对象的列表.请注意,玩家"不是列表本身,它只是对其的引用.区别在于您的列表可以是一组包含名称和地址的物理卡,而玩家"可以是一张纸,上面写着请参阅卡索引集名称和地址"".您可以有很多纸,所有纸都指同一套卡片,但它们都是不同的纸.

创建新的列表变量时,
Well, actually, you did.
This is not going to be too easy, without pictures and a quick check every now and then to see if your eyes are glazing over, but here goes...

You have a variable "players" which refers to a list of Player objects. Note that "players" is not the list itself, it is just a reference to it. The difference is that your list could be a set of physical cards containing names and addresses, and "players" could be a piece of paper saying "see card index set ''Names and Addresses''". You can have many pieces of paper, all referring to the same set of cards, but they are all different sheets of paper.

When you create a new list variable,
List<player> tempList = new List<player>();

您创建了一张新纸,但是这一次它说请参阅卡索引集临时""-它指的是另一组卡,当前为空.
然后执行以下操作:

You create a new sheet of paper, but this time it says "see card index set ''Temporary''" - it refer to a different set of cards, currently empty.
You then do this:

tempList = _players; //  <<<<<<<<<

所有这些操作是在"tempList"工作表上使用铅笔橡皮擦,用尽所有信息,并写入请参阅卡索引集名称和地址"".它复制Reference,而不是内容.

因此,当您对"tempList"工作表所引用的纸牌组进行随机化时,您还将对玩家"工作表所涉及的纸牌组进行随机化(因为它们都指的是同一张纸牌).

如果要使用另一组卡,则必须将列表复制到新位置.试试:

All this does is use a pencil eraser on the "tempList" sheet, running out all the information, and writing in "see card index set ''Names and Addresses''". It copies the Reference, not the content.

So when you randomize the set of card referred to by the "tempList" sheet, you are also randomizing the set of cards refered to by the "players" sheet (because they both refer to the same cards).

If you want to work with a different set of cards, then you have to copy the list to your new location. Try:

List<player> tempList = new List<player>();
tempList.AddRange(_players);
Player tempPlayer = new Player();


这将创建一个新列表,所有列表都引用同一张卡片(因为列表中的每个项目都不是该卡片,它也是对该卡片的引用-啊,看,你的眼睛在闪闪发光).

那有意义吗?我希望它能做到,因为这是一个非常重要的概念,如果您不理解它,将来会给自己造成真正的麻烦!


[edit]已修复<和> ;,删除了虚假的结束标记-OriginalGriff [/edit]


This will create a new list all referring to the same cards (because each item in the list is not the card, it is also a reference to the card - ah, look, your eyes are glazing over).

Does that make sense? I hope it does, because this is a very important concept, and if you don''t understand it you are going to cause yourself real trouble in the future!


[edit]Fixed < and >, removed spurious closing tags - OriginalGriff[/edit]


这篇关于参考清单问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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