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

58,679

Solution 1

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

Solution 2

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: -

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));
}
Share:
58,679
tilenslo
Author by

tilenslo

Updated on July 22, 2022

Comments

  • tilenslo
    tilenslo almost 2 years

    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.

    The problem is that map_data has the value of all elements the same. And this value is the data that comes from function PopulateDataFromFile when the i is 1999.

    What I am doing wrong? :/

  • aiapatag
    aiapatag almost 11 years
    i don't understand the answers here... it's essentially the same as what the OP does.
  • nvoigt
    nvoigt almost 11 years
    Sorry, that's wrong. Only the return value of the function decides what gets stored in the list, no matter what variable you store it in temporarily (as long as you don't make copies).
  • svick
    svick almost 11 years
    Are you saying that if you declare map_data_array outside the loop and then change it, the value in the list will also change? That's certainly not true.
  • Rune FS
    Rune FS almost 11 years
    That doesn't change anything. The declaration does not determine the storage location, only new does.
  • tilenslo
    tilenslo almost 11 years
    Thank you, I got declared "map_data_array" on the "global" scope.
  • tilenslo
    tilenslo almost 11 years
    But why this happens? Do the list actually "link" the object and not save/copy the data that this object has?
  • DGibbs
    DGibbs almost 11 years
    @tilenslo This is happening because PopulateDataFromFile(); is probably returning the same data on each iteration.
  • srsyogesh
    srsyogesh almost 11 years
    yes understood , you have to make sure that the return value from the method (PopulateDataFromFile) is always a new object instead of just changing the existing object.
  • Gauthier Boaglio
    Gauthier Boaglio almost 11 years
    Yep! That's a good synthesis, regarding the few intell we have here.
  • svick
    svick almost 11 years
    @DGibbs Not the same data. It looks like it returns the same array, but each time with different data.
  • DGibbs
    DGibbs almost 11 years
    @svick it returns the same array, but each time with different data. how could it look like the same array then? If it returns the same array then it must have the same data to be considered the same. The OP even states that the data is all the same: I have some strange problem where all my string arrays has the same value in the List
  • svick
    svick almost 11 years
    @DGibbs You call PopulateDataFromFile() once and it returns an array with some data. You call it for the second time, it takes the same array, changes the data and returns that. So, if you look at the array PopulateDataFromFile() returned right after it returns, it looks like it's returning different data each time. But it's actually returning the same array every time, so if you look at the list after the loop, you will see the same data over and over (because it's the same array).
  • DGibbs
    DGibbs almost 11 years
    @svick Hmmm ` So, if you look at the array PopulateDataFromFile() returned right after it returns, it looks like it's returning different data each time.` why am I having a hard time believing this? Since we don't have concrete code for the method we can only speculate as to its implementation.
  • svick
    svick almost 11 years
    @DGibbs Because that's what the question says: “map_data_array has always different data, I've verified that by placing the break point there and I've checked it.”
  • svick
    svick almost 11 years
    The question says differently: “map_data_array has always different data”.
  • svick
    svick almost 11 years
    I really don't see how is processing in chunks going to help. And why 11? Why not 10000000?
  • DGibbs
    DGibbs almost 11 years
    Probably has something to do with: string[] map_data_array = new string[11];... Looks to me like PopulateDataFromFile(); could be returning everything, feel free to disagree though :)
  • svick
    svick almost 11 years
    Right, I didn't realize that. But that means your code makes even less sense, since it's always going to return a single batch.
  • DGibbs
    DGibbs almost 11 years
    ??? How so? Consider a collection size of 2000. On what planet would it return a single batch?
  • DGibbs
    DGibbs almost 11 years
    @svick right, perhaps he means the data inside of the array is all the same rather than all the arrays all contain the same data. It's a bit ambiguous.
  • svick
    svick almost 11 years
    I assumed PopulateDataFromFile() always returns a 11-element array. But in any case, i still don't see any situation where batching the data could fix the issue.
  • DGibbs
    DGibbs almost 11 years
    You know what they say about assuming