请帮我看看这个小程序

guo_jianwen 2009-03-09 09:01:32
using System;

class GenericAList
{
static void Main(string[] args)
{
GElementList<int> theGElementList = new GElementList<int>() ;
for (int i = 1; i < 6; i++ )
theGElementList.AddE(i*10);

string listValue = theGElementList.GetListData();
Console.WriteLine(listValue);

GElementList<string> theGElementList1 = new GElementList<string>();

theGElementList1.AddE("A");
theGElementList1.AddE("B");
theGElementList1.AddE("C");
theGElementList1.AddE("D");
theGElementList1.AddE("E");

listValue = theGElementList1.GetListData();

Console.WriteLine(listValue);
Console.Read();
}
}
public class ElementS<T>
{
private T cElement ;


public T GetElement()
{
return cElement;
}
public void SetElement(T pElement)
{
this.cElement = pElement;
}
private ElementS<T> theElementS = null ;

public ElementS<T> GetEClass()
{
return theElementS;
}
public void SetEClass(ElementS<T> pElementS)
{
this.theElementS = pElementS;
}
}
public class GElementList<T>
{
private ElementS<T> firstElement = new ElementS<T>() ;

public void AddE(T pElement)
{
ElementS<T> newElementS = new ElementS<T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}
public string GetListData()
{
string returnValue = "" ;

do
{
returnValue = firstElement.GetElement().ToString() + " "+returnValue ;
firstElement = firstElement.GetEClass(); ;
} while (firstElement.GetEClass() != null);

return returnValue ;
}
}


此程序我不懂的是它为什么用ADDE能加像集合类一样加入元素,用firstElement。GetEClass能取得下一个元素,我怎么看GElementList<T>也都只是一个普通类,而不像是一个集合类,请大家帮我解释一下
...全文
146 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
cc_net 2009-03-13
  • 打赏
  • 举报
回复
其实思想是一样的,定义节点类,每个节点类有一共引用字段,指向其他节点。

就当指成针用就行,只是不能想指针一样去++,—-的操作。
aruaru77 2009-03-12
  • 打赏
  • 举报
回复
public void AddE(T pElement)
{
ElementS <T> newElementS = new ElementS <T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}
看到上面这行代码了没?
就是通过它把链表中的节点加入链表,实现链接~

c#里面没有指针,取而代之相同作用的是引用,本质上就是指针
guo_jianwen 2009-03-12
  • 打赏
  • 举报
回复
这个我当然知道啦,还是没回答到正题上
zhangys7981 2009-03-11
  • 打赏
  • 举报
回复
GElementList <int> theGElementList = new GElementList <int>() ;
在这里GElementList <T>被实例化为int类型的,即T是一个泛型类,可以被实例化为任意的类型。
guo_jianwen 2009-03-10
  • 打赏
  • 举报
回复
怎么没有人回哦
guo_jianwen 2009-03-10
  • 打赏
  • 举报
回复
c,c++我学过,那里面的链表是用指针,C#里面是没有的,也可以说是链表?这一套模式我不太明白
天乐 2009-03-09
  • 打赏
  • 举报
回复
链表依靠现在存储单元和指向下一个单元的引用或指针构造,不是一两句话能说清楚的

这是基本的数据结构,需要牢固掌握的

请google 数据结构 链表


知之为知之,不知google之
guo_jianwen 2009-03-09
  • 打赏
  • 举报
回复
请问这为什么是一个链?我是新手,不太明白,谢谢
lchh0917 2009-03-09
  • 打赏
  • 举报
回复
先顶,泛型最主要的优点就是性能,免去了引用类型与值类型间的转换所需的装箱和拆箱操作,且是类型安全的;
泛型的一个主要用途就是用于集合类,可以像值类型元素一样操作集合类;
天乐 2009-03-09
  • 打赏
  • 举报
回复
如果你明白链表这种数据结构,应该就知道为何它是个集合类了
天乐 2009-03-09
  • 打赏
  • 举报
回复
注意这个方法实际上是在构建一个链表

public void AddE(T pElement)
{
ElementS <T> newElementS = new ElementS <T>();
newElementS.SetElement(pElement);
newElementS.SetEClass(firstElement) ;
firstElement = newElementS ;
}

111,126

社区成员

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

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

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