Is 2-dimensional lists possible in C #?

I would like to create a multi-dimensional list. For reference, I'm working on a playlist parser.

I have a file / list file that my program saves in a standard list. One line from the file in each entry in the list.

Then I parse the regex list to find specific strings. Some data / results from the rows should be placed in a new multidimensional list; since I donโ€™t know how many results / data I can get, I cannot use a multidimensional array.

Here is the data I want to insert:

 List
 (
     [0] => List
         (
             [0] => Track ID
             [1] => Name
             [2] => Artist
             [3] => Album
             [4] => Play Count
             [5] => Skip Count

         )
     [1] => List
         (
 And so on ....

Real example:

 List
 (
     [0] => List
         (
             [0] => 2349
             [1] => The Prime Time of Your Life
             [2] => Daft Punk
             [3] => Human After All
             [4] => 3
             [5] => 2

         )
     [1] => List
         (

So, mlist [0] [0] will get the TrackID from song 1, mlist [1] [0] from song 2, etc.

But I'm having huge problems creating a multi-dimensional list. So far i've come up with

List<List<string>> matrix = new List<List<string>>(); 

But I did not have much progress :(

+80
list c #
Mar 20 '09 at 8:06
source share
9 answers

Well, of course, you can use List<List<string>> , where you then write:

 List<string> track = new List<string>(); track.Add("2349"); track.Add("The Prime Time of Your Life"); // etc matrix.Add(track); 

But why do you need to do this instead of creating your own class to represent the track, with the characteristics of Track ID, Name, Artist, Album, Play Count and Skip Count? Then simple.

+125
Mar 20 '09 at 8:11
source share

As Jon Skeet mentioned, you can do this with a List<Track> . The Track class will look something like this:

 public class Track { public int TrackID { get; set; } public string Name { get; set; } public string Artist { get; set; } public string Album { get; set; } public int PlayCount { get; set; } public int SkipCount { get; set; } } 

And to create a list of tracks as List<Track> , you simply do this:

 var trackList = new List<Track>(); 

Adding tracks can be as simple as this:

 trackList.add( new Track { TrackID = 1234, Name = "I'm Gonna Be (500 Miles)", Artist = "The Proclaimers", Album = "Finest", PlayCount = 10, SkipCount = 1 }); 

Access to tracks can be performed using the index operator:

 Track firstTrack = trackList[0]; 

Hope this helps.

+97
Mar 20 '09 at 8:20
source share

This is the easiest way I have found for this.

 List<List<String>> matrix= new List<List<String>>(); //Creates new nested List matrix.Add(new List<String>()); //Adds new sub List matrix[0].Add("2349"); //Add values to the sub List at index 0 matrix[0].Add("The Prime of Your Life"); matrix[0].Add("Daft Punk"); matrix[0].Add("Human After All"); matrix[0].Add("3"); matrix[0].Add("2"); 

Getting values โ€‹โ€‹is even easier.

 string title = matrix[0][1]; //Retrieve value at index 1 from sub List at index 0 
+32
Aug 29 '13 at 16:48 on
source share

Another work that I used around was ...

 List<int []> itemIDs = new List<int[]>(); itemIDs.Add( new int[2] { 101, 202 } ); 

The library I'm working on has a very formal class structure, and I didnโ€™t do any extra things there to privilege the recording of two "related" chains.

It relies on a programmer who introduces only an array of 2 elements, but since this is not a general element, I think it works.

+11
Jun 25 2018-12-12T00:
source share

Here's how to make a two-dimensional list

  // Generating lists in a loop. List<List<string>> biglist = new List<List<string>>(); for(int i = 1; i <= 10; i++) { List<string> list1 = new List<string>(); biglist.Add(list1); } // Populating the lists for (int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { biglist[i].Add((i).ToString() + " " + j.ToString()); } } textbox1.Text = biglist[5][9] + "\n"; 

Remember the dangers of access to a place that is not populated.

+2
Aug 15 '14 at 22:41
source share

You can also ... so

 List<List<Object>> Parent=new List<List<Object>>(); List<Object> Child=new List<Object>(); child.Add(2349); child.Add("Daft Punk"); child.Add("Human"); . . Parent.Add(child); 

if you need another element (child), create a new instance of child,

 Child=new List<Object>(); child.Add(2323); child.Add("asds"); child.Add("jshds"); . . Parent.Add(child); 
+2
Oct 07 '15 at 2:54
source share

I used:

 List<List<String>> List1 = new List<List<String>> var List<int> = new List<int>(); List.add("Test"); List.add("Test2"); List1.add(List); var List<int> = new List<int>(); List.add("Test3"); List1.add(List); 

which is equal to:

 List1 ( [0] => List2 // List1[0][x] ( [0] => Test // List[0][0] etc. [1] => Test2 ) [1] => List2 ( [0] => Test3 
+1
Nov 24
source share

You can also use DataTable - you can determine the number of columns and their types, and then add the rows http://www.dotnetperls.com/datatable

0
Feb 13 '13 at 11:10
source share

Here is something I did some time ago for the game engine I was working on. He was used as the owner of a local object. Basically, you use it like a regular list, but it holds the value in the position of what was ever the name of the string (or ID). A little modification, and you will have your own 2D list.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GameEngineInterpreter { public class VariableList<T> { private List<string> list1; private List<T> list2; /// <summary> /// Initialize a new Variable List /// </summary> public VariableList() { list1 = new List<string>(); list2 = new List<T>(); } /// <summary> /// Set the value of a variable. If the variable does not exist, then it is created /// </summary> /// <param name="variable">Name or ID of the variable</param> /// <param name="value">The value of the variable</param> public void Set(string variable, T value) { if (!list1.Contains(variable)) { list1.Add(variable); list2.Add(value); } else { list2[list1.IndexOf(variable)] = value; } } /// <summary> /// Remove the variable if it exists /// </summary> /// <param name="variable">Name or ID of the variable</param> public void Remove(string variable) { if (list1.Contains(variable)) { list2.RemoveAt(list1.IndexOf(variable)); list1.RemoveAt(list1.IndexOf(variable)); } } /// <summary> /// Clears the variable list /// </summary> public void Clear() { list1.Clear(); list2.Clear(); } /// <summary> /// Get the value of the variable if it exists /// </summary> /// <param name="variable">Name or ID of the variable</param> /// <returns>Value</returns> public T Get(string variable) { if (list1.Contains(variable)) { return (list2[list1.IndexOf(variable)]); } else { return default(T); } } /// <summary> /// Get a string list of all the variables /// </summary> /// <returns>List string</string></returns> public List<string> GetList() { return (list1); } } } 
0
Jun 14 '17 at 9:07 on
source share



All Articles