C# 带有字符串数组的列表 (List<string[]>)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17167168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:44:06  来源:igfitidea点击:

List with string array (List<string[]>)

c#arraysstringlist

提问by tilenslo

I have some strange problem where all my string arrays has the same value in the List. Here is my code:

我有一些奇怪的问题,我所有的字符串数组在列表中都具有相同的值。这是我的代码:

List<string[]> map_data = new List<string[]>();
string[] map_data_array = new string[11];

for(int i = 0; i < 2000; i++)
{
    map_data_array = PopulateDataFromFile(); // it returns different data every call
    map_data.Add(map_data_array); // store to List
}

map_data_array has always different data, I've verified that by placing the break point there and I've checked it.

map_data_array 总是有不同的数据,我已经通过在那里放置断点来验证这一点,我已经检查过它。

The problem is that map_datahas the value of all elements the same. And this value is the data that comes from function PopulateDataFromFilewhen the i is 1999.

问题是map_data所有元素的值都相同。而这个值就是PopulateDataFromFile当 i 为 1999 时来自函数的数据。

What I am doing wrong? :/

我做错了什么?:/

采纳答案by nvoigt

That only happens if you place the same array into the list. As you did not give the code to PopulateDataFromFilewe can only guess what happens. Make sure that the function returns a seperate array created with neweach time.

只有将相同的数组放入列表时才会发生这种情况。由于您没有提供代码,PopulateDataFromFile我们只能猜测会发生什么。确保该函数返回new每次创建的单独数组。

回答by srsyogesh

In the loop everytime you just change the address of map_data_array , so that's why always the value will get changed to the newer data obtained from the method call. Reinitialize the string array everytime will help. It should look something like this

在循环中,每次您只更改 map_data_array 的地址,这就是为什么值总是会更改为从方法调用获得的较新数据的原因。每次都重新初始化字符串数组会有所帮助。它应该看起来像这样

    for(int i = 0; i < 2000; i++)
    {
         string[] map_data_array = PopulateDataFromFile(); // it returns different data every call
         map_data.Add(map_data_array); // store to List
    } 

or if its confusing for you can you make it simple by

或者如果它让你感到困惑,你可以通过

    for(int i = 0; i < 2000; i++)
    {             
         map_data.Add(PopulateDataFromFile()); // store to List
    } 

回答by hakish

PopulateDataFromFile() is returning a String array with the same values.

PopulateDataFromFile() 返回一个具有相同值的字符串数组。

回答by DGibbs

You need to process your data in chunks since PopulateDataFromFile();looks to be returning all of its data in one go (or as much as the array can fit). Using an extension method, you could do something like this: -

您需要分块处理数据,因为PopulateDataFromFile();看起来一次性返回所有数据(或尽可能多地返回数组)。使用扩展方法,您可以执行以下操作:-

List<string[]> map_data = new List<string[]>();
foreach (var batch in PopulateDataFromFile().Batch(11))
{
       map_data.Add((batch.ToArray());
}

Extension method: -

扩展方法:-

public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> items, int batchSize)
{
     return items.Select((item, inx) => new { item, inx })
                 .GroupBy(x => x.inx / batchSize)
                 .Select(g => g.Select(x => x.item));
}