2维列表可能在C#?

我想build立一个多维列表。 作为参考,我正在制作一个播放列表分析器。

我有一个文件/文件列表,我的程序保存在一个标准的列表。 每个列表条目中的文件一行。

然后我用正则expression式来分析列表来查找特定的行。 一些数据/行结果需要被放入一个新的多维列表中; 因为我不知道会有多less结果/数据,所以我不能使用multidimensional array。

这是我想要插入的数据:

名单
 (
     [0] =>列表
         (
             [0] =>音轨ID
             [1] =>名字
             [2] =>艺术家
             [3] =>相册
             [4] =>播放计数
             [5] =>跳过计数

         )
     [1] =>列表
         (
等等....

实例:

列表([0] =>列表([0] => 2349 [1] =>你生命中的黄金时间=>蠢货朋克[3] =>人类总算[4] => 3 [5] => 2)[1] => List( 

所以是的,mlist [0] [0]会从歌曲1获得TrackID,从歌曲2获得mlist [1] [0]等。

但是我在创build一个多维列表时遇到了很多问题。 到目前为止,我已经想出了

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

但我还没有真正有更多的进展:(

那么你当然可以使用List<List<string>>然后你写:

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

但为什么你会这样做,而不是build立自己的class级来表示一个轨道,与轨道ID,姓名,艺术家,专辑,播放计数和跳过计数属性? 然后只需要一个List<Track>

Jon Skeet提到你可以用List<Track>来代替。 Track类看起来像这样:

 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; } } 

而要创build一个List<Track>的轨道列表,你只需要这样做:

 var trackList = new List<Track>(); 

添加曲目可以像这样简单:

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

访问轨道可以使用索引操作符来完成:

 Track firstTrack = trackList[0]; 

希望这可以帮助。

这是我发现的最简单的方法。

 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"); 

检索值更容易

 string title = matrix[0][1]; //Retrieve value at index 1 from sub List at index 0 

另一个我用过的工作是…

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

我正在工作的图书馆有一个非常正式的class级结构,我不想在那里额外的东西有效地logging两个“相关”整数的特权。

依赖于程序员只input2项数组,但因为它不是一个普通的项目,我认为它的工作原理。

我用了:

 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); 

等于:

 List1 ( [0] => List2 // List1[0][x] ( [0] => Test // List[0][0] etc. [1] => Test2 ) [1] => List2 ( [0] => Test3 

你也可以这样做,

 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); 

如果您需要另一个项目(子项目),则创build一个新的子项目实例,

 Child=new List<Object>(); child.Add(2323); child.Add("asds"); child.Add("jshds"); . . Parent.Add(child); 

您也可以使用DataTable – 您可以定义列的数量和types,然后添加行http://www.dotnetperls.com/datatable

以下是如何制作二维列表

//在循环中生成列表 List> biglist = new List>();

  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"; 

请注意访问未填充位置的危险。

这是我刚刚为我正在开发的游戏引擎所做的一些事情。 它被用作本地对象variables的持有者。 基本上,你使用它作为一个正常的列表,但它保存在什么string名称(或ID)的位置的值。 有点修改,你会有你的2D列表。

 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); } } }