0

My structures are defined like this:

List<int> Column = new List<int>();
List<List<int>> Columns = new List<List<int>>();

I'm populating the structure like this:

Columns.Clear();
for (int x=0;...
{
    Column.Clear();
    Column.Add(0); // amount of adjustment for this list
    Column.Add(x);
    for (int y=0;...
        ...
        Column.Add(data);
        Column[0] += dataAdjustment; // keep running total of adjustment
    }
    Columns.Add(Column);
}

Then, I'm trying to get the data out like this:

Columns.Sort((r1, r2) => r1[0].CompareTo(r2[0]));
Column = Columns[0];

I only want the column with the least amount of adjustment.

My problem is that in the end, Column always has in it the last column added to the list of columns and not the column with the least amount of adjustment.

What am I doing wrong? How can I get the list with the least amount of adjustment running total?

1 Answer 1

6

What am I doing wrong?

You're reusing (and overwriting) the same Column for every iteration of your for loop. You need to allocate a new List<int>(); for each new column, so instead of:

for (int x=0;...
{
    Column.Clear();

Do:

for (int x=0;...
{
    List<int> Column = new List<int>();
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick. Thanks. I assumed that when I was done with the column (by adding it to the Columns list) that I could reuse it. I guess not.

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.