求高手解答:C#基础问题
编程背景资料:
public class Corporation
{
public string Num;
public string Name;
public string Post;
public string Notes;
}
编程要求:编写一个针对以上数据定义类的集合类;实现集合成员的新增、插入、修改、查询、定位、移除、清除,按Numb索引,Numb和Name索引等功能。
[解决办法]
有点时间帮你写了一下,使用List基本能够完成你要的功能,我只是封装了一下,还需要什么功能自己加吧。
//written by wqxhome
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
using System;
using System.Collections.Generic;
using System.Text;
public class CorporationNode
{
string num;
string name;
string post;
string notes;
public string Num
{
get
{
return num;
}
set
{
num = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Post
{
get
{
return post;
}
set
{
post = value;
}
}
public string Notes
{
get
{
return notes;
}
set
{
notes = value;
}
}
public override string ToString()
{
return num + ", " + name + ", " + post + ", " + notes;
}
}
public class CorporationList
{
List <CorporationNode> corporations;
public CorporationList()
{
corporations = new List <CorporationNode> ();
}
public CorporationNode this[int index]
{
get
{
if (index > corporations.Count)
{
throw new Exception( "越界 ");
}
return corporations[index];
}
}
public void Add(CorporationNode corporationNode)
{
corporations.Add(corporationNode);
}
public void Insert(int index, CorporationNode corporationNode)
{
corporations.Insert(index, corporationNode);
}
public void ModifyAt(int index, CorporationNode newCorporationNode)
{
CorporationNode c = corporations[index];
c.Num = newCorporationNode.Num;
c.Name = newCorporationNode.Name;
c.Post = newCorporationNode.Post;
c.Notes = newCorporationNode.Notes;
}
public void Remove(CorporationNode corporationNode)
{
corporations.Remove(corporationNode);
}
public void RemoveAt(int index)
{
corporations.RemoveAt(index);
}
public CorporationNode FindByNum(string num)
{
foreach (CorporationNode node in corporations)
{
if (node.Num == num)
{
return node;
}
}
return null;
}
public override String ToString()
{
StringBuilder sb = new StringBuilder();
foreach (CorporationNode node in corporations)
{
sb.Append(node.Num);
sb.Append( ", ");
sb.Append(node.Name);
sb.Append( ", ");
sb.Append(node.Post);
sb.Append( ", ");
sb.Append(node.Notes);
sb.Append( "\r\n ");
}
return sb.ToString();
}
//余下有什么功能自己加吧
}
public class App
{
public static void Main()
{
CorporationNode node = new CorporationNode();
node.Num = "100 ";
node.Name = "wqxhome ";
node.Post = "post ";
node.Notes = "notes ";
//添加元素
CorporationList list = new CorporationList();
list.Add(node);
//打印添加结果
System.Console.Write(list.ToString());
//修改元素
CorporationNode node2 = new CorporationNode();
node2.Num = "1001 ";
node2.Name = "wqxhome ";
node2.Post = "post ";
node2.Notes = "notes ";
list.ModifyAt(0, node2);
//修改0元素后打印
System.Console.Write(list.ToString());
//通过Num查找
CorporationNode node3 = list.FindByNum( "1001 ");
System.Console.Write(node3.ToString());
System.Console.Read();
}
}
}