0

I need to create a list of integer arrays. I know ahead of time the length of the arrays, but I don't know how many of them need to be added to the list.

I've tried the following code:

    List<int[]> MyListOfArrays = new List<int[]>(); 
    int[] temp = new int[30]; 
    range = xlApp.get_Range("NamedRange");  
    values = (object[,])range.Value2;
    for (int i = 0; i < values.GetLength(0); i++)
    {
        for (int j = 0; j < values.GetLength(1); j++)
        {
            temp[j] = Convert.ToInt32(values[i + 1, j + 1]);  
        }
        MyListOfArrays.Add(temp);
    }

The temp array is filled just fine. However, MyListOfArrays just ends up with the last iteration of temp repeated for all of the entries. Where am I going wrong?

7
  • 3
    I don't know, there are a few cases where a List<int[]> might make sense. I don't have enough info to know whether this is a valid case, or would benefit from a proper data model. Commented Sep 9, 2013 at 20:09
  • 1
    @TimS what's next? List<Dictionary<List<string>,Dictionary<double,string>>>[]?? Commented Sep 9, 2013 at 20:13
  • Not efficient but your temp variable could be a List<int> and then convert it to array using .ToArray() Commented Sep 9, 2013 at 20:16
  • 1
    Can you point me in the direction of what you mean by a proper data model? I'm new to this arena and am learning as I go. I'm using this code to replace cumbersome calculations that are currently being done in Excel. Commented Sep 9, 2013 at 20:18
  • 2
    @Mathnv "create a proper data model" refers to the process of making an abstraction of objects from the real world (for example Customer, Product and so on) and creating classes that represent these conceptual entities with their characteristics and behavior (in the form of properties, methods and events) Commented Sep 9, 2013 at 20:25

3 Answers 3

4

When you add the temp array to the List, it is just a pointer to the array created on the heap. You need to create a new temp array for every array you add to the list.

List<int[]> MyListOfArrays = new List<int[]>();
range = xlApp.get_Range("NamedRange");  
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
    int[] temp = new int[30];  // moved inside the loop
    for (int j = 0; j < values.GetLength(1); j++)
    {
        temp[j] = Convert.ToInt32(values[i + 1, j + 1]);  
    }
    MyListOfArrays.Add(temp);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That solved it. I moved the int[] temp = new int[NumReels * NumRows]; line inside the outer for loop.
@MathNV Just removed my identical answer - but just note the issue was the scope of the temp. You were re-initializing the same object every time, with each element in the list having a reference to the same array. Now you are creating and initializing a new object on each iteration.
0

Here's the easiest fix.

Your code:

List<int[]> MyListOfArrays = new List<int[]>(); 
int[] temp = new int[30];// <-- Move this inside the 'i' for-loop.
range = xlApp.get_Range("NamedRange");  
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
    for (int j = 0; j < values.GetLength(1); j++)
    {
        temp[j] = Convert.ToInt32(values[i + 1, j + 1]);  
    }
    MyListOfArrays.Add(temp);
}

Do this instead:

List<int[]> MyListOfArrays = new List<int[]>(); 
range = xlApp.get_Range("NamedRange");  
values = (object[,])range.Value2;
for (int i = 0; i < values.GetLength(0); i++)
{
    int[] temp = new int[30]; //<-- This will create a new array of ints, with each iteration of 1.
    for (int j = 0; j < values.GetLength(1); j++)
    {
        temp[j] = Convert.ToInt32(values[i + 1, j + 1]);  
    }
    MyListOfArrays.Add(temp);
}

Comments

0

You just need to move the line:

int[] temp = new int[30]; 

immediately after:

for (int i = 0; i < values.GetLength(0); i++) {

so it initializes to a new array at each iteration of the loop.

MyListOfArrays.Add is adding the same reference to you list for each iteration of i, and it overwrites the values in temp[] each time. So you end up with the values of the last iteration repeated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.