Nested List ExampleUse a nested List to store data in two dimensions. Nested lists are like jagged or 2D lists.
This page was last reviewed on Feb 19, 2023.
List, nested. A List can have elements of List type. This is a jagged list, similar in syntax to a jagged array. Lists are not by default multidimensional in C#.
Shows a list
2D list solution. To store more complex shapes of data, we can develop multidimensional Lists. We use an easily-developed and expandable data structure.
Requirements. Consider this shape: the first row has 3 elements, the next row just has 1, and then we have a row with 4 elements. We require a nested list solution that allows this data.Shows a list
0 1 2 2 1 2 3 4
Example code. The C# List 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.
Part 1 We create a list, and add sub-lists to it. The first List will contain an internal array of List references.
Part 2 This method receives a parameter of type List<List<int>>. It uses for each to loop over the inner contents of each List.
Part 3 We can access a specific element within a nested list. The top-level list is accessed first, and then the sub-list.
Info The total count of a nested list must be computed by accessing all the sub lists from the top-level list.
Tip You can sometimes simplify List syntax by using the implicit typing provided by the var-keyword.
using System; using System.Collections.Generic; class Program { static void Main() { // Part 1: add rows and columns to the List. List<List<int>> list = new List<List<int>>(); var rand = new Random(); for (int i = 0; i < 3; i++) { // Put some integers in the inner lists. List<int> sublist = new List<int>(); int top = rand.Next(1, 4); for (int v = 0; v < top; v++) { sublist.Add(rand.Next(1, 5)); } // Add the sublist to the top-level List. list.Add(sublist); } // Display the List. Display(list); } static void Display(List<List<int>> list) { // Part 2: loop over and 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(); } // Part 3: display element. Console.WriteLine("Element at 1, 0:"); Console.WriteLine(list[1][0]); // Part 4: display total count. int count = 0; foreach (var sublist in list) { count += sublist.Count; } Console.WriteLine("Count:"); Console.WriteLine(count); } }
Elements: 1 3 2 2 2 2 Element at 1, 0: 2 Count: 6
A discussion. As for performance, a nested List 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.
Jagged Arrays
A 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 it helps in prototyping or experimental work.
Flatten Array
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
© 2007-2023 Sam Allen.