【问题标题】:I have a loop that initializes a list of objects with values, but as soon as it exits the loop the objects become identical我有一个循环,它用值初始化一个对象列表,但是一旦它退出循环,对象就会变得相同
【发布时间】:2014-04-06 16:13:23
【问题描述】:

正如标题所示,我正在通过循环初始化列表中的对象。但是当循环退出时,它们都变得相同。我可以在循环中看到它们不一样。但是当循环退出时,它们会变为最后输入的对象。

public List<ElevationLayout> layoutList = new List<ElevationLayout>();
    public int layoutNumber { get; set; }
    public int worldWidth { get; set; }

    public Random seed { get; set; }
    public XYSize dimLeft { get; set; }


//I have narrowed down the problem to this method
    //==========================================================================================================================================================
    //==========================================================================================================================================================
    //==========================================================================================================================================================

    public void init(World world) {
        dimLeft = new XYSize();
        ElevationLayout layout = new ElevationLayout();
        dimLeft.y = 0;
        dimLeft.x = world.size.x;
        seed = new Random((int)DateTime.Now.Ticks);
        worldWidth = (int)((world.size.x / 6.4) + (world.size.x / 64) - 1);
        layoutNumber =  worldWidth + seed.Next(-2, 3);
        for (int i = 0; i < layoutNumber; i++)
        {
            layout.type = seed.Next(0, 2);
            layout.width = (world.size.x / layoutNumber) + seed.Next(0, ((dimLeft.x / layoutNumber) / 2) + 1);

            if (layout.width > dimLeft.x)
            {
                layout.width = dimLeft.x;
            }

            dimLeft.x -= layout.width;
            layout.height = seed.Next(world.size.y / 16, (world.size.y / 4) + 1);

            if (layout.height > dimLeft.y)
            {
                layout.height = dimLeft.y;
            }


            this.layoutList.Add(layout);
            Console.Write(this.layoutList[i].type); // here the objects are different

            if ((world.size.y -= layout.height) > dimLeft.y)
            {
                dimLeft.y = (world.size.y - layout.height);
            }

            if (dimLeft.x <= 0)
            {
                layoutNumber = i;
            }
        }
        Console.WriteLine();
                for (int y = 0; y < layoutNumber; y++)
                    Console.Write(this.layoutList[y].type); //but as soon as i exit the loop they are the same
    }

    //==============================================================================================================
    //==============================================================================================================
    //==============================================================================================================

这里有人遇到了类似的问题:Why is my list of objects all the same? 在这里:Why are all the values in my list the same?

最初我的对象列表是静态的,但后来我删除了它,问题仍然存在。

【问题讨论】:

    标签: c#


    【解决方案1】:

    问题是您只在循环之前创建对象的一个​​实例并设置该对象的属性。 修复:

        for (int i = 0; i < layoutNumber; i++)
        {
              ElevationLayout layout = new ElevationLayout();
    

    基本上是在循环内创建对象,这样每次迭代都会分配一个新对象。

    【讨论】:

    • 感谢修复它。如果我每次都更改对象的值然后添加它,您能解释一下为什么会出现问题吗? (作为一名编程学生,我只是好奇。)
    • 参见msdn.microsoft.com/en-us/library/4d43ts61(v=vs.90).aspx - 当您将对象添加到列表时,您添加的是对它的引用而不是它的副本。所以你有一个完整的列表,其中包含对单个对象(即内存中的单个对象)的引用。
    • 谢谢,这很有意义。我会阅读您发布的那篇文章(?)。
    【解决方案2】:

    您一遍又一遍地将相同的布局添加到列表中,而不是每次都创建一个新的布局实例。移动代码以在循环内创建布局对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2021-12-25
      • 2012-04-05
      相关资源
      最近更新 更多