2d list csharp

Code Example - 2d list csharp

                
                        List<List<int>> matrix = new List<List<int>>();
List<int> list1 = new List<int>();
list1.Add("4");
list1.Add("5");
matrix.Add(list1);
List<int> list2 = new List<int>();
list2.Add("6");
list2.Add("7");
matrix.Add(list2);

//matrix is now
// 4 5
// 6 7
                    
                
 

csharp 2d list

                        
                                //This code is an updated, working version of the code Tender Termite wrote

List<List<int>> matrix = new List<List<int>>();  //Create 2d List
List<int> list1 = new List<int>();
list1.Add(4);
list1.Add(5);
matrix.Add(list1);	//Insert List into matrix
List<int> list2 = new List<int>();
list2.Add(6);
list2.Add(7);
matrix.Add(list2);

int x = matrix[0][1];	//Get data from Matrix, here x is 5

matrix[0].RemoveAt(1);  //Removes the 5
x = matrix[0][0];		//x is now 4

matrix.RemoveAt(0);		//removes whole first row

x = matrix[0][1];		//x is now 7
                            
                        
 

Related code examples