HomeSearch

C# Nested Lists: Create 2D List or Jagged List

Use a nested List to store data in two dimensions. Nested lists are like jagged or 2D lists.
List, nested. A List can have elements of List type. This is a jagged list, similar in syntax to a jagged array. We can develop multidimensional Lists. We use an easily-developed and expandable data structure.
Example. The List generic type in C# provides an underlying and internal array where it stores its elements. But it always specifies a one-dimensional and zero-based array in its internal structure.List

Note: When you create a List whose element type is another List, the first List will contain an internal array of List references.

And: The List objects these references point to are separate. They will contain one-dimensional arrays.

Var: You can sometimes simplify this syntax by using the implicit typing provided by the var-keyword.

Var

Display: This method receives a parameter of type List<List<int>>. It uses the foreach-loop syntax to loop over the inner contents of each List.

ForeachConsole.WriteLine
C# program that uses nested Lists using System; using System.Collections.Generic; class Program { static void Main() { // // Add rows and columns to the List. // List<List<int>> list = new List<List<int>>(); var rand = new Random(); for (int i = 0; i < 10; i++) { // // Put some integers in the inner lists. // List<int> sublist = new List<int>(); int top = rand.Next(1, 15); for (int v = 0; v < top; v++) { sublist.Add(rand.Next(1, 5)); } // // Add the sublist to the top-level List reference. // list.Add(sublist); } // // Display the List. // Display(list); } static void Display(List<List<int>> list) { // // Display everything in the List. // Console.WriteLine("Elements:"); foreach (var sublist in list) { foreach (var value in sublist) { Console.Write(value); Console.Write(' '); } Console.WriteLine(); } // // Display element at 3, 3. // if (list.Count > 3 && list[3].Count > 3) { Console.WriteLine("Element at 3, 3:"); Console.WriteLine(list[3][3]); } // // Display total count. // int count = 0; foreach (var sublist in list) { count += sublist.Count; } Console.WriteLine("Count:"); Console.WriteLine(count); } } Output Elements: 4 2 4 1 2 1 3 2 4 2 4 3 3 1 4 2 4 1 2 1 2 1 3 4 4 3 2 4 2 1 2 4 4 3 1 4 3 4 4 3 2 1 4 1 2 4 1 3 2 2 1 3 1 4 1 4 2 2 3 2 1 2 Element at 3, 3: 1 Count: 62
Discussion. A nested List type is relatively fast. It internally uses zero-based arrays, and the List type itself is fast. But adding another layer of abstraction and creating a nested class may be desirable.

Note: This can help reduce the amount of complexity and syntax confusion at the calling sites.

Tip: Another option is to encapsulate the List<List<T>> into a separate class. This could be better for real-world programs.

Class
Summary. We used a nested List that is essentially a jagged or 2D List data type with the generic List class. The code here is not ideal but is fine for prototyping or experimental work.Jagged Arrays
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls