Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 重复将相同的随机值插入到集合C中#_C#_Dictionary_For Loop - Fatal编程技术网

C# 重复将相同的随机值插入到集合C中#

C# 重复将相同的随机值插入到集合C中#,c#,dictionary,for-loop,C#,Dictionary,For Loop,这可能有点傻,但我被困在这上面了。我有一个具有默认值的属性的列表。我要做的是使用随机值更新列表中的属性,然后将列表中的新值存储在字典中。字典的键是int,表示年份,值是列表。问题是字典中只存储了最后一组随机值。请检查我的密码 public static void Main() { List<int> numbers = new List<int>(){ 0, 1, 2, 3 ,4}; Dictionary<int, List<TestClass

这可能有点傻,但我被困在这上面了。我有一个具有默认值的属性的列表。我要做的是使用随机值更新列表中的属性,然后将列表中的新值存储在字典中。字典的键是int,表示年份,值是列表。问题是字典中只存储了最后一组随机值。请检查我的密码

public static void Main()
{
    List<int> numbers = new List<int>(){ 0, 1, 2, 3 ,4};
    Dictionary<int, List<TestClass>> testAllYearsList = new Dictionary<int, List<TestClass>>();
    int year = 2018;
    var random = new Random();

    List<TestClass> testList = new List<TestClass>();

    testList = db;//assuming the values here are fetched from db

    foreach(var number in numbers){

        testList.ForEach(x => {
            x.test = random.NextDouble();
        });
        testAllYearsList.Add(year - number, testList);

    }

    foreach(KeyValuePair<int, List<TestClass>> entry in testAllYearsList)
    {
            foreach(var xx in entry.Value){
                Console.WriteLine(entry.Key + "-------" + xx.test);
            }
    }
}
public class TestClass{
    public string name{get;set;}
    public double test{get;set;}
}
我所期待的是这样的:

2018-------0.232323558231521
2018-------0.679072365236698
2017-------0.702259658231545
2017-------0.834268732412351
2016-------0.465468889231561
2016-------0.323423456811698
2015-------0.125332658231528
2015-------0.347588566678699
2014-------0.734758565656521
2014-------0.854544444571690
更新:我试过了,但还是遇到了同样的问题

foreach(var number in numbers){
    var newTestList = new List<TestClass>();
    foreach(var xx in testList){
    newTestList.Add(xx);        
    }
    newTestList.ForEach(x => {
        x.test = random.NextDouble();
    });
    testAllYearsList.Add(year - number, newTestList);

}
foreach(数值中的变量编号){
var newTestList=新列表();
foreach(测试列表中的变量xx){
新测试列表。添加(xx);
}
newTestList.ForEach(x=>{
x、 test=random.NextDouble();
});
添加(年份编号,新测试列表);
}

当您将相同的
测试列表
与相同的
测试类
对象一起使用时,您要将对相同对象的引用添加到
测试年度列表
,解决此问题的一种可能方法是每次添加一个新的
测试列表

static void Main(string[] args) {
    List<int> numbers = new List<int>() { 0, 1, 2, 3, 4 };
    Dictionary<int, List<TestClass>> testAllYearsList = new Dictionary<int, List<TestClass>>();
    int year = 2018;
    var random = new Random();

    foreach (var number in numbers) {
        List<TestClass> testList = new List<TestClass> {
            new TestClass { test = 0.2 },
            new TestClass { test = 1.5 }
        };
        testList.ForEach(x => {
            x.test = random.NextDouble();
        });
        testAllYearsList.Add(year - number, testList);

    }

    foreach (KeyValuePair<int, List<TestClass>> entry in testAllYearsList) {
        foreach (var xx in entry.Value) {
            Console.WriteLine(entry.Key + "-------" + xx.test);
        }
    }
}

当您将相同的
testList
与相同的
TestClass
对象一起使用时,将对相同对象的引用添加到
testAllYearsList
,解决此问题的一种可能方法是每次添加一个新的
testList
对象:

static void Main(string[] args) {
    List<int> numbers = new List<int>() { 0, 1, 2, 3, 4 };
    Dictionary<int, List<TestClass>> testAllYearsList = new Dictionary<int, List<TestClass>>();
    int year = 2018;
    var random = new Random();

    foreach (var number in numbers) {
        List<TestClass> testList = new List<TestClass> {
            new TestClass { test = 0.2 },
            new TestClass { test = 1.5 }
        };
        testList.ForEach(x => {
            x.test = random.NextDouble();
        });
        testAllYearsList.Add(year - number, testList);

    }

    foreach (KeyValuePair<int, List<TestClass>> entry in testAllYearsList) {
        foreach (var xx in entry.Value) {
            Console.WriteLine(entry.Key + "-------" + xx.test);
        }
    }
}

您只有两个
TestClass
实例,您已经将这两个实例的引用添加到字典中的每个条目中。当
TestClass
的一个实例的属性被更新时,对该
TestClass
实例的所有引用都会看到相同的值

numbers
的循环中创建
TestClass
的新实例,并查看您的预期行为:

testList = new List<TestClass> {
   new TestClass { test = random.NextDouble() },
   new TestClass { test = random.NextDouble() }
}
testAllYearsList.Add(year - number, testList);
testList=新列表{
新测试类{test=random.NextDouble()},
新测试类{test=random.NextDouble()}
}
添加(年份-编号,测试列表);

您只有两个
TestClass
实例,您已经将这两个实例的引用添加到字典中的每个条目中。当
TestClass
的一个实例的属性被更新时,对该
TestClass
实例的所有引用都会看到相同的值

numbers
的循环中创建
TestClass
的新实例,并查看您的预期行为:

testList = new List<TestClass> {
   new TestClass { test = random.NextDouble() },
   new TestClass { test = random.NextDouble() }
}
testAllYearsList.Add(year - number, testList);
testList=新列表{
新测试类{test=random.NextDouble()},
新测试类{test=random.NextDouble()}
}
添加(年份-编号,测试列表);

现在终于可以工作了。以下是我所改变的:

foreach (var number in numbers)
        {
            var newTestList = new List<TestClass>();
            foreach (var xx in testList)
            {
                newTestList.Add(new TestClass
                {
                    name = xx.name,
                    test = xx.test
                });
            }
            newTestList.ForEach(x => {
                x.test = random.NextDouble();
            });
            testAllYearsList.Add(year - number, newTestList);

        }
foreach(数值中的变量编号)
{
var newTestList=新列表();
foreach(测试列表中的变量xx)
{
添加(新的测试类
{
name=xx.name,
test=xx.test
});
}
newTestList.ForEach(x=>{
x、 test=random.NextDouble();
});
添加(年份编号,新测试列表);
}
因此,直接将
xx
添加到
newTestList
不会解决问题。相反,我需要创建新的
TestClass
,映射
xx
中的值,然后将其添加到
newTestList


非常感谢那些花时间帮忙的人。没有这些想法,我无法让它工作。

现在终于让它工作了。以下是我所改变的:

foreach (var number in numbers)
        {
            var newTestList = new List<TestClass>();
            foreach (var xx in testList)
            {
                newTestList.Add(new TestClass
                {
                    name = xx.name,
                    test = xx.test
                });
            }
            newTestList.ForEach(x => {
                x.test = random.NextDouble();
            });
            testAllYearsList.Add(year - number, newTestList);

        }
foreach(数值中的变量编号)
{
var newTestList=新列表();
foreach(测试列表中的变量xx)
{
添加(新的测试类
{
name=xx.name,
test=xx.test
});
}
newTestList.ForEach(x=>{
x、 test=random.NextDouble();
});
添加(年份编号,新测试列表);
}
因此,直接将
xx
添加到
newTestList
不会解决问题。相反,我需要创建新的
TestClass
,映射
xx
中的值,然后将其添加到
newTestList


非常感谢那些花时间帮忙的人。如果没有这些想法,我就无法实现这一点。

您继续编辑相同的
testList
,并且
testAllYearsList
的成员包含对该列表项的引用您有相同的两个您不断修改的对象,即每次获胜的最后一个随机数。尝试执行以下语句:
Console.WriteLine(ReferenceEquals(testAllYearsList[2018],testAllYearsList[2017])
,您有一个列表,可以使用不同的年份键添加到字典中,但它是相同的列表实例,因此是相同的两个TestClass实例。您继续编辑相同的
testList
,并且
testAllYearsList
的成员包含对该列表项的引用您有相同的两个不断修改的对象,每次获胜的最后一个随机数。尝试执行以下语句:
Console.WriteLine(ReferenceEquals(testAllYearsList[2018],testAllYearsList[2017]),您有一个列表,可以使用不同的年份键添加到字典中,但它是相同的列表实例,因此是相同的两个TestClass实例。例如,如果TestClass确实包含除“test”之外的其他属性,并且该列表已从db填充。我应该怎么做才能只更新测试变量,使其具有唯一的随机值?@fmpsagara此注释没有足够的空间来解释值语义和引用语义之间的差异。理解这种差异将解释为什么要有不同的状态就需要不同的实例。简短回答:添加对列表的引用不会复制/克隆/复制/复制添加的对象/If(例如