1

I was trying to use a List<List<int>> Code :

List<List<int>> list2D = new List<List<int>>();
List<int> NULLLIST = new List<int>();

NULLLIST.AddRange(Enumerable.Repeat(0, 6));
list2D.AddRange(Enumerable.Repeat(NULLLIST, 6));

for (int i = 1; i <= 5; i++)
    for (int j = 1; j <= 5; j++)
        list2D[i][j] = (5 * (i-1)) + j;

for (int i = 1; i <= 5; i++, Console.WriteLine())
    for (int j = 1; j <= 5; j++)
        Console.Write(list2D[i][j].ToString() + ' ');

Console.ReadLine();

I thought the output would be like this

1 2 3 4 5 
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

but the output was

21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25

I don't know what is wrong and how to get the first output.

shA.t
  • 16,580
  • 5
  • 54
  • 111
lomache kana
  • 15
  • 1
  • 5

4 Answers4

2

Try to avoid mixing Linq and loops, if you start generating list2D with Linq keep on doing it:

int lineCount = 5;
int colCount = 5;

var list2D = Enumerable
  .Range(0, lineCount)   // 5 lines
  .Select(line => Enumerable
     .Range(0, colCount) // 5 columns
     .Select(column => colCount * line + column + 1)
     .ToList())
  .ToList();

Test

string test = string.Join(Environment.NewLine, list2D
  .Select(line => string.Join(" ", line)));

Console.WriteLine(test);

Outcome:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
2

The point is in this line: Related answer

list2D.AddRange(Enumerable.Repeat(NULLLIST, 6));

I think in above code we have 6 pointer to one clone of NULLLIST
in each iteration of i you are updating that clone of NULLLIST it means you have:

1 2 3 4 5  //=> NULLLIST clone: {1,2,3,4,5}
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 
1 2 3 4 5 

then

6 7 8 9 10  //=> NULLLIST clone : {6,7,8,9,10}
6 7 8 9 10 
6 7 8 9 10 
6 7 8 9 10 
6 7 8 9 10 

and at last:

21 22 23 24 25  //=> NULLLIST clone: {21,22,23,24,25}
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25

You can use this way:

var list2D = new List<List<int>>();

for (var i = 0; i < 5; i++)
{
    var row = new List<int>();
    for (var j = 0; j < 5; j++)
        row.Add(5*i + j + 1);

    list2D.Add(row);
}

for (var i = 0; i < 5; i++, Console.WriteLine())
    for (var j = 0; j < 5; j++)
        Console.Write($"{list2D[i][j],5}");
shA.t
  • 16,580
  • 5
  • 54
  • 111
1

You are using the same NULLLIST 5 times. If you want to use loops, you can use a loop to initialize the lists:

list2D.Add(new List<int>()); // (*)

for (int i = 1; i <= 5; i++)
{
    var NULLLIST = new List<int>();
    NULLLIST.AddRange(Enumerable.Repeat(0, 6));
    list2D.Add(NULLLIST);
}

Why have I added the line marked with (*)?

Normally, lists index start at 0, so an initial padding element is required. So, two additional advices:

  • try to use 0-based indexes, otherwise its confusing
  • try to use arrays, it'll get easier

Idea:

int[,] list2D = new int[5, 5];

for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
        list2D[i, j] = (5 * i) + (j + 1);

for (int i = 0; i < 5; i++, Console.WriteLine())
    for (int j = 0; j < 5; j++)
        Console.Write(list2D[i, j].ToString() + ' ');
ventiseis
  • 3,029
  • 11
  • 32
  • 49
0
var list2D = Enumerable.Range(0, 5).Select(i => 
                                         Enumerable.Range(i * 5 + 1, 5).ToList()).ToList();

or a bit shorter:

var list2D = new int[5].Select((_, i) => Enumerable.Range(i * 5 + 1, 5).ToList()).ToList();

foreach (var list in list2D) 
    Console.WriteLine(string.Join("\t", list));
Slai
  • 22,144
  • 5
  • 45
  • 53