c# 中是否可以使用二维列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/665299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are 2 dimensional Lists possible in c#?
提问by CasperT
I'd like to set up a multidimensional list. For reference, I am working on a playlist analyzer.
我想建立一个多维列表。作为参考,我正在研究播放列表分析器。
I have a file/file-list, which my program saves in a standard list. One line from the file in each list entry.
我有一个文件/文件列表,我的程序将其保存在标准列表中。每个列表条目中的文件中的一行。
I then analyze the list with regular-expressions to find specific lines. Some of the data/results from the lines needs to be put into a new multidimensionallist; since I don't know how many results/data I'll end up with, I can't 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 yeah, mlist[0][0] would get TrackID from song 1, mlist[1][0] from song 2 etc.
所以是的,mlist[0][0] 会从歌曲 1 中获取 TrackID,从歌曲 2 中获取 mlist[1][0] 等等。
But I am having huge issues creating a multidimensional list. So far I have come up with
但是我在创建多维列表时遇到了很大的问题。到目前为止,我想出了
List<List<string>> matrix = new List<List<string>>();
But I haven't really had much more progress :(
但我并没有真正取得更多进展:(
采纳答案by Jon Skeet
Well you certainly canuse a List<List<string>>
where you'd then write:
那么你当然可以使用一个List<List<string>>
你会写的地方:
List<string> track = new List<string>();
track.Add("2349");
track.Add("The Prime Time of Your Life");
// etc
matrix.Add(track);
But why would you do that instead of building your own class to represent a track, with Track ID, Name, Artist, Album, Play Count and Skip Count properties? Then just have a List<Track>
.
但是为什么要这样做而不是构建自己的类来表示具有轨道 ID、名称、艺术家、专辑、播放次数和跳过次数属性的曲目?然后只需一个List<Track>
.
回答by Spoike
As Jon Skeetmentioned you can do it with a List<Track>
instead. The Track class would look something like this:
正如Jon Skeet提到的,你可以用 aList<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; }
}
And to create a track list as a List<Track>
you simply do this:
要创建曲目列表,List<Track>
您只需执行以下操作:
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
});
Accessing tracks can be done with the indexing operator:
可以使用索引运算符来访问轨道:
Track firstTrack = trackList[0];
Hope this helps.
希望这可以帮助。
回答by paul jamison
another work around which i have used 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 wan't extra stuff in there effectively for the privilege of recording two 'related' ints.
我正在使用的库有一个非常正式的类结构,我不想为了记录两个“相关”整数的特权而在那里有效地添加额外的东西。
Relies on the programmer entering only a 2 item array but as it's not a common item i think it works.
依赖于程序员只输入一个 2 项数组,但由于它不是一个常见的项目,我认为它有效。
回答by SoIAS
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);
that equals:
这等于:
List1
(
[0] => List2 // List1[0][x]
(
[0] => Test // List[0][0] etc.
[1] => Test2
)
[1] => List2
(
[0] => Test3
回答by Val
You can also use DataTable - you can define then the number of columns and their types and then add rows http://www.dotnetperls.com/datatable
您还可以使用 DataTable - 您可以定义列数及其类型,然后添加行 http://www.dotnetperls.com/datatable
回答by Jordan LaPrise
This is the easiest way i have found to do it.
这是我发现的最简单的方法。
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");
To retrieve values is even easier
检索值更容易
string title = matrix[0][1]; //Retrieve value at index 1 from sub List at index 0
回答by Ben
Here is how to make a 2 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";
Be aware of the danger of accessing a location that is not populated.
请注意进入无人居住的位置的危险。
回答by DDK
You can also..do in this way,
你也可以..这样做,
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 item(child), create a new instance of child,
如果您需要另一个项目(子项),请创建一个新的子项实例,
Child=new List<Object>();
child.Add(2323);
child.Add("asds");
child.Add("jshds");
.
.
Parent.Add(child);
回答by Joe Horrell
Here's a little something that I made a while ago for a game engine I was working on. It was used as a local object variable holder. Basically, you use it as a normal list, but it holds the value at the position of what ever the string name is(or ID). A bit of modification, and you will have your 2D list.
这是我不久前为我正在开发的游戏引擎制作的一些东西。它被用作本地对象变量持有者。基本上,您将它用作普通列表,但它保存字符串名称(或 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);
}
}
}