C#中的多维列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1222117/
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
Multidimensional Lists in C#
提问by Mike B
At the moment I am using one list to store one part of my data, and it's working perfectly in this format:
目前我正在使用一个列表来存储我的数据的一部分,并且它以这种格式完美运行:
Item
----------------
Joe Bloggs
George Forman
Peter Pan
Now, I would like to add another line to this list, for it to work like so:
现在,我想在这个列表中添加另一行,让它像这样工作:
NAME EMAIL
------------------------------------------------------
Joe Bloggs [email protected]
George Forman [email protected]
Peter Pan [email protected]
I've tried using this code to create a list within a list, and this code is used in another method in a foreach loop:
我已经尝试使用此代码在列表中创建一个列表,并且此代码用于 foreach 循环中的另一种方法:
// Where List is instantiated
List<List<string>> list2d = new List<List<string>>
...
// Where DataGrid instance is given the list
dg.DataSource = list2d;
dg.DataBind();
...
// In another method, where all people add their names and emails, then are added
// to the two-dimensional list
foreach (People p in ppl.results) {
list.Add(results.name);
list.Add(results.email);
list2d.Add(list);
}
When I run this, I get this result:
当我运行这个时,我得到这个结果:
Capacity Count
----------------
16 16
16 16
16 16
... ...
Where am I going wrong here. How can I get the output I desire with the code I am using right now?
我哪里出错了。如何使用我现在使用的代码获得我想要的输出?
采纳答案by Thomas Levesque
Why don't you use a List<People>
instead of a List<List<string>>
?
为什么不使用 aList<People>
而不是 a List<List<string>>
?
回答by Lasse V. Karlsen
Please show more of your code.
请显示更多您的代码。
If that last piece of code declares and initializes the list
variable outside the loop you're basically reusing the same list object, thus adding everything into one list.
如果最后一段代码list
在循环外声明并初始化变量,您基本上是在重用相同的列表对象,从而将所有内容添加到一个列表中。
Also show where .Capacity and .Count comes into play, how did you get those values?
还要显示 .Capacity 和 .Count 在哪里发挥作用,你是如何获得这些值的?
回答by Winston Smith
Where does the variable results come from?
变量结果从何而来?
This block:
这个块:
foreach (People p in ppl.results) {
list.Add(results.name);
list.Add(results.email);
list2d.Add(list);
}
Should probably read more like:
应该更像是:
foreach (People p in ppl.results) {
var list = new List<string>();
list.Add(p.name);
list.Add(p.email);
list2d.Add(list);
}
回答by Nakul Chaudhary
You should use List<Person>
or a HashSet<Person>
.
您应该使用List<Person>
或HashSet<Person>
.
回答by Sailing Judo
Highly recommend something more like this:
强烈推荐更像这样的东西:
public class Person {
public string Name {get; set;}
public string Email {get; set;}
}
var people = new List<Person>();
Easier to read, easy to code.
易于阅读,易于编码。
回答by Rickey
It's old but thought I'd add my two cents... Not sure if it will work but try using a KeyValuePair:
它很旧,但我想我会加两分钱......不确定它是否会起作用,但尝试使用 KeyValuePair:
List<KeyValuePair<?, ?>> LinkList = new List<KeyValuePair<?, ?>>();
LinkList.Add(new KeyValuePair<?, ?>(Object, Object));
You'll end up with something like this:
你最终会得到这样的结果:
LinkList[0] = <Object, Object>
LinkList[1] = <Object, Object>
LinkList[2] = <Object, Object>
and so on...
等等...
回答by Shimmy Weitzhandler
If for some reason you don't want to define a Person
class and use List<Person>
as advised, you can use a tuple, such as (C# 7):
如果由于某种原因您不想定义一个Person
类并List<Person>
按照建议使用,您可以使用一个元组,例如 (C# 7):
var people = new List<(string Name, string Email)>
{
("Joe Bloggs", "[email protected]"),
("George Forman", "[email protected]"),
("Peter Pan", "[email protected]")
};
var georgeEmail = people[1].Email;
The Name
and Email
member names are optional, you can omit them and access them using Item1
and Item2
respectively.
在Name
和Email
成员名称是可选的,你可以忽略它们,并将它们使用访问Item1
和Item2
分别。
There are defined tuples for up to 8 members.
定义了最多 8 个成员的元组。
For earlier versions of C#, you can still use a List<Tuple<string, string>>
(or preferably ValueTuple
using this NuGet package), but you won't benefit from customized member names.
对于早期版本的 C#,您仍然可以使用 a List<Tuple<string, string>>
(或最好ValueTuple
使用此 NuGet 包),但您不会从自定义成员名称中受益。