【发布时间】:2019-09-28 08:16:55
【问题描述】:
我有 4 个问题:
我怎么可能打印 Console.WriteLine(list.head.Next.Next.Data)。 从 AddHead() 开始,新对象将保存在前一个对象之上 n.Next?
我创建了一个自定义类来尝试了解上面的示例是否只是特定于列表的。 那么,我怎么可能 Console.WriteLine(cc.head.Next.Next.Next.Data) ?
如何在论坛的盒子里贴代码,这是我第一次发帖,请见谅。
谢谢!
using System;
namespace GenericInterfaces
{
class Program
{
static void Main()
{
//Declare and instantiate a new generic SortedList class.
//Person is the type argument.
SortedList<Person> list = new SortedList<Person>();
//Create name and age values to initialize Person objects.
string[] names = new string[]
{
"Franscoise",
"Bill",
"Li",
"Sandra",
"Gunnar",
"Alok",
"Hiroyuki",
"Maria",
"Alessandro",
"Raul"
};
int[] ages = new int[] { 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
//Populate the list with new Node Objects
for (int x = 0; x < 10; x++)
{
//ages[x] is integer
list.AddHead(new Person(names[x],ages[x]));
}
//Print out unsorted list.
foreach (var p in list)
{
System.Console.Write(p.ToString() + " ");
Console.WriteLine("Unsorted Data");
Console.WriteLine(list.head.Data);
Console.WriteLine(list.head.Next.Data);
Console.WriteLine(list.head.Next.Next.Data);
Console.WriteLine(list.head.Next.Next.Next.Data);
}
System.Console.WriteLine("Done with unsorted list");
Console.WriteLine("My Custom Class Experiment");
MyCustomClass cc = new MyCustomClass();
for (int i = 0; i < ages.Length; i++)
{
cc.AddHead(ages[i]);
}
Console.WriteLine("Added ages to custom class");
Console.WriteLine(cc.head.Data);
Console.WriteLine(cc.head.Next.Data);
Console.WriteLine(cc.head.Next.Next.Data);
Console.WriteLine(cc.head.Next.Next.Next.Data);
Console.WriteLine(cc.head.Next.Next.Next.Next.Data);
Console.WriteLine(cc.n.Data);
Console.WriteLine(cc.n.Next.Data);
}
}
//Type parameter T in angle brackets.
public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
{
public Node head;
public Node current = null;
// Nested class is also generic on T
public class Node
{
public Node Next { get; set; } = null;
public T Data { get; set; }
public Node(T t) //T used in non-generic constructor
{
Data = t;
}
}
public GenericList() //constructor
{
head = null;
}
public void AddHead(T t) //T as method parameter type
{
Node n = new Node(t);
n.Next = head;
head = n;
}
// Implementation of the iterator
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
// IEnumerable<T> inherits from IEnumerable, therefore this class
// must implement both the generic and non-generic versions of
// GetEnumerator. In most cases, the non-generic method can
// simply call the generic method.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class MyCustomClass
{
public Mode head;
public Mode current = null;
public Mode n;
// Nested class is also generic on T
public class Mode
{
//public Mode Next { get; set; } = null;
public Mode Next;
public int Data { get; set; }
public Mode(int t) //T used in non-generic constructor
{
Data = t;
}
}
public void AddHead(int t) //T as method parameter type
{
n = new Mode(t);
n.Next = head;
head = n;
}
}
【问题讨论】:
-
我格式化了代码。如果您愿意,可以通过单击帖子下方的“已编辑”链接来比较两个版本。 (代码通过将所有内容缩进 4 个空格来标记。标记代码并单击顶部的
{}symbol 即可。) -
您好 Peter A. Schneider,感谢您的评论,以后会试一试!