0

Microsoft は、ジェネリックを学習するためのバブル ソートの例としてこれを示しています。76 行目と 77 行目に到達するまでは意味があります。これらの宣言はどのようにして可能になるのでしょうか? ノードはクラスです。newでインスタンス化する必要はありませんか?並べ替えをどのように最適化しますか? 汎用部品と非汎用部品はどれですか?

1   public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
2       {
3           protected Node head;
4           protected Node current = null;
5   
6           // Nested class is also generic on T
7           protected class Node
8           {
9               public Node next;
10              private T data;  //T as private member datatype
11   
12              public Node(T t)  //T used in non-generic constructor
13              {
14                  next = null;
15                  data = t;
16              }
17  
18              public Node Next
19              {
20                  get { return next; }
21                  set { next = value; }
22              }
23  
24              public T Data  //T as return type of property
25              {
26                  get { return data; }
27                  set { data = value; }
28              }
29          }
30  
31          public GenericList()  //constructor
32          {
33              head = null;
34          }
35  
36          public void AddHead(T t)  //T as method parameter type
37          {
38              Node n = new Node(t);
39              n.Next = head;
40              head = n;
41          }
42  
43          // Implementation of the iterator
44          public System.Collections.Generic.IEnumerator<T> GetEnumerator()
45          {
46              Node current = head;
47              while (current != null)
48              {
49                  yield return current.Data;
50                  current = current.Next;
51                  
52              }
53          }
54  
55          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
56          {
57              return GetEnumerator();
58          }
59      }
60  
61      public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
62      {
63          // A simple, unoptimized sort algorithm that 
64          // orders list elements from lowest to highest:
65  
66          public void BubbleSort()
67          {
68              if (null == head || null == head.Next)
69              {
70                  return;
71              }
72              bool swapped;
73  
74              do
75              {
76                  Node previous = null;
77                  Node current = head;
78  
79                  
80                  //Console.WriteLine(previous.GetType());
81                  //Console.ReadLine();
82  
83                  swapped = false;
84                  
85                   
86                  //Debug.WriteLine(p);
87                  //Debug.WriteLine("Testing " + current.ToString());
88  
89                  while (current.next != null)
90                  {
91                      //  Because we need to call this method, the SortedList
92                      //  class is constrained on IEnumerable<T>
93                      if (current.Data.CompareTo(current.next.Data) > 0)
94                      {
95                          Node tmp = current.next;
96                          current.next = current.next.next;
97                          tmp.next = current;
98  
99                          if (previous == null)
100                         {
101                             head = tmp;
102                         }
103                         else
104                         {
105                             previous.next = tmp;
106                         }
107                         previous = tmp;
108                         swapped = true;
109                     }
110                     else
111                     {
112                         previous = current;
113                         current = current.next;
114                     }
115                 }
116             } while (swapped);
117         }
118     }
4

4 に答える 4

2

C#の型は、または宣言と互換性のある型の値でclass初期化できます。null76行目と77行目は次のようになります

Node previous = null;
Node current = head;

ここで値nullは合法です。つまり「私には価値がない」ということです。もタイプ であるheadため、への代入も有効です。結果は 2 つの参照であり、同じオブジェクト値を参照しています。参照の 1 つを介してインスタンスを変更すると、他の参照に表示されます。headNodeheadcurrentNodeNode

于 2011-10-27T15:42:23.957 に答える
0

previousandnextnulland にheadそれぞれ割り当てています。これは、他の操作と同様に代入操作です。オブジェクトの新しいインスタンスを実際に作成する場合を除き、new を使用する必要はありません。

ジェネリック パーツは、 を参照するものTです。これは、クラスがインスタンス化されるときに提供されるジェネリック型です。良い例はList<T>、タイプのオブジェクトのリストを作成する ですTList<string>これは、文字列List<int>のリスト、int のリストなどとしてインスタンス化できます。

于 2011-10-27T15:41:41.367 に答える
0

これらは、クラスのインスタンスを指している可能性のある参照、または単に.null

参照を他の参照に割り当てることができます。

Foo f1 = new Foo();
Foo f2 = f1;
Foo f3 = null;
于 2011-10-27T15:43:35.043 に答える
0

そのコードの 76 行目と 77 行目では、インスタンス化されているオブジェクトはありません。おっしゃるとおりNodeクラスです。.NET のクラスは常に参照型です。つまり、次のことを意味します。

  1. 参照型の変数の値は、オブジェクトへの参照です (または何もありません)。
  2. 参照型の変数には value を割り当てることができますnull。これは、変数が何も参照しないことを意味します。
  3. 参照型の 1 つの変数を参照型の別の変数に代入すると、オブジェクトではなく参照がコピーされます。
于 2011-10-27T15:47:01.047 に答える