复合类型LIST如何取元素?

antinet 2011-07-05 02:58:05
如下定义:
public List<List<int>> GridsArry = new List<List<int>>();

第1次,向队列中增加序列1,3,5,7;,然后将这个序列增加到GridsArry;
第2次又增加序列8,9,10,再将这个序列增加到GridsArry
此时,可以用COUNT方法得到有2个序列,但怎么分别取这个序列的元素呢?
...全文
227 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
jshzp 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 antinet 的回复:]
看错了,现在项目是.NET FRAMEWORK2.0,设成3.5可以了
[/Quote]
吼吼,楼主辛苦,可把您累坏了!
antinet 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 fangxinggood 的回复:]
grids.Clear();

grids 和 ga[0] 是同一引用。

再一次 ga.Add(grids) 就变成 ga[0], ga[1] 是同一个引用。

所以不要搞 grids.Clear();

而是要再定义一个容器 List<int> grids_2 = new List<int>();
再加。
[/Quote]
没明白,grids和ga[0]怎么是同一引用?再定义个一个容器grids_2,然后ga.Add(grids_2)?
那后续的第3,第4个队列呢?
antinet 2011-07-05
  • 打赏
  • 举报
回复
看错了,现在项目是.NET FRAMEWORK2.0,设成3.5可以了
机器人 2011-07-05
  • 打赏
  • 举报
回复
grids.Clear();

grids 和 ga[0] 是同一引用。

再一次 ga.Add(grids) 就变成 ga[0], ga[1] 是同一个引用。

所以不要搞 grids.Clear();

而是要再定义一个容器 List<int> grids_2 = new List<int>();
再加。
q107770540 2011-07-05
  • 打赏
  • 举报
回复
.NET FRAMEWORK 4.0 没有LINQ? 谁说的?
antinet 2011-07-05
  • 打赏
  • 举报
回复
我时.NET FRAMEWORK 4.0,没有LINQ,另外,通过如下程序
List<int> grids = new List<int>();
List<List<int>> ga = new List<List<int>>();

grids.Add(10);
grids.Add(13);
grids.Add(14);
ga.Add(grids);

grids.Clear();

grids.Add(7);
grids.Add(8);
ga.Add(grids);

可以验证,用2次ADD方法向List<List<int>>中增加一个数列,第2次的会覆盖第一次的。
List<List<int>>貌似是个2维数组,后一个ADD方法将导致维数重新调整,从而丢失前面的数据
itliyi 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bdmh 的回复:]
遍历GridsArry,其中每个元素都是List<int>,然后每个元素还可以取索引
foreach(List<int> list in GridsArry)
{
foreach(int i in list)
{
......
}
}
[/Quote]Y
antinet 2011-07-05
  • 打赏
  • 举报
回复
string ss = "";
foreach (int gg in Grids)
{
ss = ss + gg.ToString() + " ";
}
MessageBox.Show(ss,"共"+Grids.Count.ToString() + "个grid被选中");
//***上面可以正确的显示出当前Grids的整数序列
//** 下面,将Grids这个序列追加到GridsArry,用foreach 发现,每次都是最后一个序列追加进去,前面的被覆盖

//** 然后将这个LIST追加到LIST
if (Grids.Count > 0)
{
GridsArry.Add(Grids);

int i = 0;

MessageBox.Show("共 " + GridsArry.Count.ToString() + " gridarry", "");

//GridsArry.Select
foreach (List<int> gd in GridsArry)
{
ss = "";
foreach (int a in gd)
{
ss = ss + a.ToString() + " ";
}
MessageBox.Show(ss, "第" + i.ToString() + "区");
i++;
}

//MessageBox.Show(ss, "第" + i.ToString() + "区");

}

q107770540 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 antinet 的回复:]

另外,在C#,VS2010中,没有SELECTMANY这个方法吧?
[/Quote]
Using System.Linq

http://msdn.microsoft.com/en-us/library/bb534336.aspx
antinet 2011-07-05
  • 打赏
  • 举报
回复
另外,在C#,VS2010中,没有SELECTMANY这个方法吧?
moonwrite 2011-07-05
  • 打赏
  • 举报
回复
你干什么要用个这样的类型
不试试字典什么的???
antinet 2011-07-05
  • 打赏
  • 举报
回复
//** 然后将这个LIST追加到LIST
if (Grids.Count > 0)
{
GridsArry.Add(Grids);

int i = 0;

MessageBox.Show("共 " + GridsArry.Count.ToString() + " gridarry", "");

foreach (List<int> gd in GridsArry)
{
ss = "";
foreach (int a in gd)
{
ss = ss + a.ToString() + " ";
}
MessageBox.Show(ss, "第" + i.ToString() + "区");
i++;
}

//MessageBox.Show(ss, "第" + i.ToString() + "区");

}
这样每次将整数序列追加到当前序列,但跟踪显示LIST<LIST>里面只有最后一次追加的内容,前面的被覆盖。但LIST<LIST>却能正确的计算出 有几个序列
flyerwing 2011-07-05
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fangxinggood 的回复:]
GridsArry[0], GridsArry[1]
[/Quote]
GridsArry[0][0]
GridsArry[1][0]
这样估计也成吧.
q107770540 2011-07-05
  • 打赏
  • 举报
回复

void Main()
{
List<List<int>> GridsArry = new List<List<int>>{
new List<int>{1,2,3,4},
new List<int>{5,6,7,9}
};
GridsArry.SelectMany(s=>s).ToList().ForEach(s=>Console.WriteLine(s)); //8
/*
1
2
3
4
5
6
7
9

*/
}
q107770540 2011-07-05
  • 打赏
  • 举报
回复
.SelectMany()
antinet 2011-07-05
  • 打赏
  • 举报
回复
private void SaveList(List<List<int>> ls)
{
string ss = "";
int i = 0;

MessageBox.Show("共 " + ls.Count.ToString() + " grid", "");

foreach (List<int> gd in ls)
{
ss = "";
foreach (int a in gd )
{
ss = ss + a.ToString() + " ";
}
MessageBox.Show(ss, "第" + i.ToString() + "区");
i++;
}

}
这样显示出来的都是最后一次追加的序列
bdmh 2011-07-05
  • 打赏
  • 举报
回复
遍历GridsArry,其中每个元素都是List<int>,然后每个元素还可以取索引
foreach(List<int> list in GridsArry)
{
foreach(int i in list)
{
......
}
}
机器人 2011-07-05
  • 打赏
  • 举报
回复
GridsArry[0], GridsArry[1]

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧