c#泛型中如何添加一个得到链表中节点的对象
classStudent{publicstringname;publicintage1;publicStudent(stringname,intage1){this.na...
class Student
{
public string name;
public int age1;
public Student(string name, int age1)
{
this.name = name;
this.age1 = age1;
}
}
List<Student> dinosaurs = new List<Student>();
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
dinosaurs.Add(new Student("Tyrannosaurus", 33));
dinosaurs.Add(new Student("Amargasaurus", 44));
dinosaurs.Add(new Student("Mamenchisaurus", 55));
dinosaurs.Add(new Student("Deinonychus", 66));
dinosaurs.Add(new Student("Compsognathus", 77));
for (int i = 0; i < dinosaurs .Count ; i++)
{
Student temp = dinosaurs.( 在这里需要实现一个方法,来获得链表指定节点的对象)
Console.ReadLine("{0}{1}", temp.name, temp.age1);
}
因为在c#的泛型中不支持获取链表指定节点的对象的方法,我也不知道该如何去实现他! 展开
{
public string name;
public int age1;
public Student(string name, int age1)
{
this.name = name;
this.age1 = age1;
}
}
List<Student> dinosaurs = new List<Student>();
Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);
dinosaurs.Add(new Student("Tyrannosaurus", 33));
dinosaurs.Add(new Student("Amargasaurus", 44));
dinosaurs.Add(new Student("Mamenchisaurus", 55));
dinosaurs.Add(new Student("Deinonychus", 66));
dinosaurs.Add(new Student("Compsognathus", 77));
for (int i = 0; i < dinosaurs .Count ; i++)
{
Student temp = dinosaurs.( 在这里需要实现一个方法,来获得链表指定节点的对象)
Console.ReadLine("{0}{1}", temp.name, temp.age1);
}
因为在c#的泛型中不支持获取链表指定节点的对象的方法,我也不知道该如何去实现他! 展开
1个回答
展开全部
for (int i = 0; i < dinosaurs .Count ; i++)
{
Student temp = dinosaurs[i];//这样就可以了
Console.ReadLine("{0}{1}", temp.name, temp.age1);
}
你可以参考下面的,不同的方法得到对象
class person {
public person(int uid)
{
this.uid = uid;
}
public person() {
}
public person(int uid, string name)
{
}
public person(string name,int uid)
{
}
public int uid,height;
public string name, head, hand, mouth, ear, eye;
}
class Program
{
static void Main(string[] args)
{
List<person> persons = new List<person>();
for (int i = 0; i < 8; i++)
persons.Add(new person(i,"name"+i));
List<person>.Enumerator er = persons.GetEnumerator();
while (er.MoveNext())
{
Console.WriteLine(er.Current.name);
}
for (int i = 0; i < persons.Count; i++)
{
person temp = null;
temp = persons[i];
if (temp != null)
Console.WriteLine("{0}'s uid is {1}", temp.name, temp.uid);
}
Console.ReadLine();
}
}
{
Student temp = dinosaurs[i];//这样就可以了
Console.ReadLine("{0}{1}", temp.name, temp.age1);
}
你可以参考下面的,不同的方法得到对象
class person {
public person(int uid)
{
this.uid = uid;
}
public person() {
}
public person(int uid, string name)
{
}
public person(string name,int uid)
{
}
public int uid,height;
public string name, head, hand, mouth, ear, eye;
}
class Program
{
static void Main(string[] args)
{
List<person> persons = new List<person>();
for (int i = 0; i < 8; i++)
persons.Add(new person(i,"name"+i));
List<person>.Enumerator er = persons.GetEnumerator();
while (er.MoveNext())
{
Console.WriteLine(er.Current.name);
}
for (int i = 0; i < persons.Count; i++)
{
person temp = null;
temp = persons[i];
if (temp != null)
Console.WriteLine("{0}'s uid is {1}", temp.name, temp.uid);
}
Console.ReadLine();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询