在c#中是否可以使用二维列表?
我想建立一个多维列表。 作为参考,我正在使用播放列表分析器 我有一个文件/文件列表,我的程序将其保存在标准列表中。每个列表条目中文件的一行 然后,我用正则表达式分析列表以找到特定的行。 行中的一些数据/结果需要放入新的多维列表中;因为我不知道最终会得到多少结果/数据,所以我不能使用多维数组 以下是我要插入的数据: List ( [0] => List ( [0] => Track ID [1] => Name [2] => Artist [3] => Album [4] => Play Count [5] => Skip Count ) [1] => List ( And so on.... 列表 ( [0]=>列表 ( [0]=>曲目ID [1] =>名称 [2] =>艺术家 [3] =>相册 [4] =>播放计数 [5] =>跳过计数 ) [1] =>列表 ( 等等 实例: 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 ( 列表 ( [0]=>列表 ( [0] => 2349 [1] =>你生命中的黄金时期 [2] =>愚蠢的朋克 [3] =>毕竟是人类 [4] => 3 [5] => 2 ) [1] =>列表 ( 是的,mlist[0][0]会从歌曲1中获取TrackID,从歌曲2中获取mlist[1][0]等等 但我在创建多维列表时遇到了巨大的问题。 到目前为止,我已经想出了在c#中是否可以使用二维列表?,c#,list,C#,List,我想建立一个多维列表。 作为参考,我正在使用播放列表分析器 我有一个文件/文件列表,我的程序将其保存在标准列表中。每个列表条目中文件的一行 然后,我用正则表达式分析列表以找到特定的行。 行中的一些数据/结果需要放入新的多维列表中;因为我不知道最终会得到多少结果/数据,所以我不能使用多维数组 以下是我要插入的数据: List ( [0] => List ( [0] => Track ID [1] => Name
List<List<string>> matrix = new List<List<string>>();
列表矩阵=新列表();
但我并没有取得太大的进步:(当然,你可以使用列表,然后写:
List<string> track = new List<string>();
track.Add("2349");
track.Add("The Prime Time of Your Life");
// etc
matrix.Add(track);
List track=new List();
轨道。添加(“2349”);
添加(“你生命中的黄金时期”);
//等
矩阵。添加(跟踪);
但是,为什么您要这样做,而不是构建自己的类来表示一首曲目,其中包含曲目ID、名称、艺术家、唱片集、播放计数和跳过计数属性?然后只需创建一个列表,如前所述,您可以使用列表来完成此操作。曲目类的外观如下所示:
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; }
}
要将曲目列表创建为列表
,只需执行以下操作:
var trackList = new List<Track>();
可以使用索引操作符访问轨迹:
Track firstTrack = trackList[0];
希望这能有所帮助。我使用的另一项工作是
List<int []> itemIDs = new List<int[]>();
itemIDs.Add( new int[2] { 101, 202 } );
List itemIDs=new List();
Add(新的int[2]{101202});
我正在使用的库有一个非常正式的类结构,我没有为了记录两个“相关”的int而有效地在其中添加额外的内容
依赖于程序员只输入一个2项数组,但由于它不是一个常用项,我认为它可以工作。我使用:
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);
您还可以使用DataTable—您可以定义列的数量及其类型,然后添加行
这是我发现的最简单的方法
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");
下面是如何制作二维列表
// 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";
//在循环中生成列表。
List biglist=新列表();
对于(inti=1;i你也可以这样做
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);
List Parent=new List();
列表子项=新列表();
儿童。添加(2349);
添加(“愚蠢的朋克”);
儿童。添加(“人类”);
.
.
父。添加(子);
如果您需要其他物品(儿童),
创建子对象的新实例
Child=new List<Object>();
child.Add(2323);
child.Add("asds");
child.Add("jshds");
.
.
Parent.Add(child);
Child=newlist();
儿童。添加(2323);
儿童。添加(“asds”);
添加(“jshds”);
.
.
父。添加(子);
这是我不久前为正在开发的游戏引擎制作的一个小东西。它被用作局部对象变量保持器。基本上,您可以将其用作普通列表,但它将值保持在字符串名称(或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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间GameEngineInterpreter
{
公共类变量列表
{
私人名单1;
私人名单2;
///
///初始化一个新的变量列表
///
公共变量列表()
{
list1=新列表();
list2=新列表();
}
///
///设置变量的值。如果变量不存在,则创建该变量
///
///变量的名称或ID
///变量的值
公共无效集(字符串变量,T值)
{
如果(!list1.Contains(变量))
{
列表1.Add(变量);
清单2.增加(价值);
}
其他的
{
list2[list1.IndexOf(变量)]=值;
}
}
///
///删除变量(如果存在)
///
///变量的名称或ID
公共无效删除(字符串变量)
{
if(列表1.Contains(变量))
{
列表2.RemoveAt(列表1.IndexOf(变量));
列表1.RemoveAt(列表1.IndexOf(变量));
}
}
///
///清除变量列表
///
公共空间清除()
{
清单1.Clear();
清单2.Clear();
}
///
///获取变量(如果存在)的值
///
///变量的名称或ID
///价值观
公共T Get(字符串变量)
{
if(列表1.Contains(变量))
{
返回(列表2[列表1.IndexOf(变量)]);
}
其他的
{
返回默认值(T);
}
}
///
///获取所有变量的字符串列表
///
///列表字符串
公共列表GetList()
{
返回(列表1);
}
}
}
只是因为还没有提到它,有时我更喜欢列表。有些情况下,我只是不想出于任何原因创建自定义对象,而且这种超简单的数据结构非常灵活。我意识到一切都是一个字符串,效率不高,它迫使你在增加播放计数时解析和字符串化,但对某些人来说,这可能是值得的
var trackList = new List<Dictionary<string, string>>();
var track = new Dictionary<string, string>();
track.Add("TrackID" , "1234");
track.Add("Name" , "I'm Gonna Be (500 Miles)");
track.Add("Artist" , "The Proclaimers");
track.Add("Album" , "Finest");
track.Add("PlayCount", "10");
track.Add("SkipCount", "1");
trackList.Add(track);
var trackList=newlist();
var track=newdictionary();
track.Add(“TrackID”,“1234”)
Child=new List<Object>();
child.Add(2323);
child.Add("asds");
child.Add("jshds");
.
.
Parent.Add(child);
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);
}
}
}
var trackList = new List<Dictionary<string, string>>();
var track = new Dictionary<string, string>();
track.Add("TrackID" , "1234");
track.Add("Name" , "I'm Gonna Be (500 Miles)");
track.Add("Artist" , "The Proclaimers");
track.Add("Album" , "Finest");
track.Add("PlayCount", "10");
track.Add("SkipCount", "1");
trackList.Add(track);