如何ArrayList里面再放ArrayList?

wtoeb 2007-01-20 06:47:00
以下是两个arraylist,第一个即Items,第二个即Item:
public struct Items
{
public int X;//第x层
public int Y;//第y列
public bool Z;//z是否显示
public Items(int x, int y,bool z)
{
X = x;
Y = y;
Z = z;
}
}
public struct Item
{
public int A;//第a层
public int B;//第b列
public bool C;//是否显示
public Item(int a, int b,bool c)
{
A = a;
B = b;
C = c;
}
}
现要求如:
当通过
ArrayList arr = new ArrayList();
arr.Clear();
arr.Add(new Items(0,0,false));//Items的第一项
arr.Add(new Items(0,1,false));//Items的第二项
arr.Add(new Items(1,0,false));//Items的第三项
arr.Add(new Items(1,1,false));//Items的第四项
后,可以对Items的第一项至第四项添加Item。即形成:
A B C X Y Z
0 0 false 数据0 数据a 数据p
0 0 false 数据1 数据b 数据q
0 0 false 数据2 数据c 数据r
0 0 false 数据3 数据d 数据s
0 0 false 数据4 数据e 数据t
……
...全文
380 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wtoeb 2007-01-24
  • 打赏
  • 举报
回复
再顶一下。
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
ArrayList arrs = new ArrayList();
arrs.Clear();
arrs.Add(new Node(0,0,false));//指示下级
arrs.Add(new Node(0,1,false));//指示下级
arrs.Add(new Node(1,0,false));//指示下级
arrs.Add(new Node(1,1,false));//指示下级
for(int i = 0; i < arrs.Count; i ++)
{
Node al = (Node)arrs[i];
msg += al.A + " - " + al.B + " - " + al.C + "<br>\n";
ArrayList arr = new ArrayList();
//开始循环读取
Node node=new Node(9,9,true);//怎么new传参将看你自己
if(arr.Count==0||((Node)arr[arr.Count]).C!=node.C)
{
arr.Add(node);
}
else
{
((Node)arr[arr.Count]).Add(node);
}
for(int j = 0; j < arr.Count; j++)
{
Node cs = (Node)arr[j];
msg += cs.A;
}
}
这样的结果好像是对的哈,我已经晕头转向了。
he_8134 2007-01-20
  • 打赏
  • 举报
回复
ArrayList arr = new ArrayList();
//开始循环读取
Node node=new(0,0,true);//怎么new传参将建看你自己
if(arr.Count==0||((Node)arr[arr.Count]).C!=node.C)
{
arr.Add(node);
}else{
((Node)arr[arr.Count]).Add(node);
}
//结束循环????
he_8134 2007-01-20
  • 打赏
  • 举报
回复
唉,又错一点if(arr.Count>0&&((Node)arr[arr.Count]).C!=node.C)

应该是if(arr.Count==0||((Node)arr[arr.Count]).C!=node.C)
he_8134 2007-01-20
  • 打赏
  • 举报
回复
上面错了

ArrayList arr = new ArrayList();
arr.Clear();
//开始循环读取
Node node=new(0,0,true);//怎么new传参将建看你自己
if(arr.Count>1&&((Node)arr[arr.Count]).C!=node.C)
{
arr.Add(node);
}else{
((Node)arr[arr.Count]).Add(node);
}
//结束循环,这样吗???
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
因为这样的要求,我不得不把查询的结果进行顺序分组,即1-4,5-8,9,……
然后根据分组,查找子分组中的有多少条数据,然后对这些数据进行格式显示。即某组分类数据显示几列。
he_8134 2007-01-20
  • 打赏
  • 举报
回复
ArrayList arr = new ArrayList();
arr.Clear();
Node node=new(0,0,true);
//开始循环读取
if(arr.Count>1&&((Node)arr[arr.Count]).C!=node.C)
{
arr.Add(node);
}else{
((Node)arr[arr.Count]).Add(node);
}
//结束循环,这样吗???
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
id type name
1 page test1
2 page test2
3 page test3
4 page test4
5 cate test0
6 cate test1
7 cate test2
8 cate test3
9 page test4

这里面是这样的,如1-4是一个page分组,系统要实现,如每行显示3个,那我得把1-4的page显示一行,还剩余一下,则用空白填满这一行。

下以下的cate是一样的:cate是5-8,则每行显示3个,那第8个cate就得显示在下一行。

最后一个page是9,那显示在web页面上就得显示成一个整行。

客户要求这样做,实在没办法。

你觉得除了这个方法还有其它不BT的方法么?请指点一下。
谢谢,太感谢了。
he_8134 2007-01-20
  • 打赏
  • 举报
回复
唉`~我还是不知道你想干什么
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
对的,客户对系统要求就是这样的,没办法,用DataTable是很占内存的。
he_8134 2007-01-20
  • 打赏
  • 举报
回复
我知道你想做什么了~~不过方法好bt哦~~~我不参与了~你好自为之~~
建议用好一点的方法~~

type每变一次,主ArrayList就加一个"组"对象~~
然后在下一次type变化之前所有项目都加到这个"组"对象里面~~

你是想这样吧?
id=10的时候如果type是cate~则又要添加一个"组"了~~
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
行了,太感谢了,救命恩人。再请教一下,
以下是往Node中添加数据,并显示ArrayList中的数据:
ArrayList arr = new ArrayList();
arr.Clear();
arr.Add(new Node(0,0,false));//指示下级
arr.Add(new Node(0,1,false));//指示下级
arr.Add(new Node(1,0,false));//指示下级
arr.Add(new Node(1,1,false));//指示下级
for(int iis = 0; iis < arr.Count; iis ++)
{
Node al = (Node)arr[iis];
msg += al.A + " - " + al.B + " - " + al.C + "<br>\n";
}
问题是,如何主ArrayList的子ArrayList在哪里呢?
应该怎么添加数据呢?再就是怎么显示出来?
太谢谢了。
he_8134 2007-01-20
  • 打赏
  • 举报
回复
1.1没范型我知道,不知道有没有索引器,List应该有吧?

public class Node
{
public Node Parent = null;
ArrayList Son = null;
public int A;
public int B;
public bool C;
public Node(int A,int B,bool C) {
this.A = A;
this.B = B;
this.C = C;
}
public void Add(Node node){
if (Son == null) Son = new ArrayList();
Son.Add(node);
node.Parent = this;
}
public Node this[int index]{
get{
return (Node)Son[index];
}
}
public int Count {
get {
return Son.Count;
}
}
}
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
问题的来源是这样的:
我从数据库中查到如下数据:
id type name
1 page test1
2 page test2
3 page test3
4 page test4
5 cate test0
6 cate test1
7 cate test2
8 cate test3
9 page test4
……
现在,要将type为page(id为1-4),type为cate(id为5-8),type为page(id为9)的分成三组(后面如果有更多的数据,将会自动进行顺序分组),分别将page、cate、page加入到Items这个主ArrayList中去。
这个主ArrayList数据分别为:
a b c
page (即id为1-4的记录)
cate (即id为5-8的记录)
page (即id为9的记录)
……
而子在ArrayList中数据分别为:
x y z
page 1
page 2
page 3
page 4
cate 5
cate 6
cate 7
cate 8
page 5
……
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
用List吗?但是我的.net是1.1的咯,
2.0不支持。
he_8134 2007-01-20
  • 打赏
  • 举报
回复
郁闷 搞了半天都运行不了
原来把Item类名改了就行了,原来是跟类库名称有点冲突....
he_8134 2007-01-20
  • 打赏
  • 举报
回复
为了防止别人直接访问Son的方法~可以改成这样

public class Item
{
public Item Parent = null;
List<Item> Son = null;
public int A;
public int B;
public bool C;
public Item(int A,int B,bool C) {
this.A = A;
this.B = B;
this.C = C;
}
public void Add(Item item){
if (Son == null) Son = new List<Item>();
Son.Add(item);
item.Parent = this;
}
public Item this[int index]{
get{
return Son[index];
}
}
public int Count {
get {
return Son.Count;
}
}
}
he_8134 2007-01-20
  • 打赏
  • 举报
回复
public class Item
{
public Item Parent = null;
List<Item> Son = null;
public int A;
public int B;
public bool C;
public Item(int A,int B,bool C) {
this.A = A;
this.B = B;
this.C = C;
}
public void AddNode(Item item){
if (Son == null) Son = new List<Item>();
Son.Add(item);
item.Parent = this;
}
public List<Item> Items {
get {
return Son;
}
}
}
wtoeb 2007-01-20
  • 打赏
  • 举报
回复
就是,如何将通过Items对Item添加数据。
即,Items是主ArrayList,Item是子ArrayList。
如何在Items中,将Item放进去呢?
谢谢。

110,533

社区成员

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

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

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