我的情況
- 僅使用類創建物件串列
這就是代碼的作業方式
- 使用該類,
ListRatings我可以創建一個串列Rating(不使用 c# 通過庫 System.Collections.Generic 提供的串列方法)
(您可以在下面的代碼中看到哪些屬性和方法具有類ListRatings和類)Rating
問題
當我嘗試列印我添加到串列中的所有評分時,我的程式只列印第一個和最后一個!
我的代碼
類 ListRating:
public class ListRatings
{
private Rating first;
private Rating last;
public ListRatings()
{
first = null;
last = null;
}
public void InsertNewRat(Rating rating)
{
if (first == null)
{
first = rating;
last = rating;
}
else
{
first.setNext(rating);
last.setNext(rating);
}
}
public void PrintRats()
{
Rating e = first;
Console.WriteLine(e.getMatter());
Console.WriteLine(e.getDate());
Console.WriteLine(e.getRate());
e = e.getNext();
Console.WriteLine(e.getMatter());
Console.WriteLine(e.getDate());
Console.WriteLine(e.getRate());
e = e.getNext();
Console.WriteLine(e.getMatter());
Console.WriteLine(e.getDate());
Console.WriteLine(e.getRate());
}
}
班級評價:
public class Rating
{
private int rate;
private string matter;
private DateTime date;
private Rating next;
public Rating(int rate, string matter, DateTime date)
{
this.rate = rate;
this.matter = matter;
this.date = date;
next = null;
}
public int getRate()
{
return rate;
}
public string getMatter()
{
return matter;
}
public DateTime getDate()
{
return date;
}
public Rating getNext()
{
return next;
}
public void setNext(Rating valutazione)
{
next = valutazione;
}
}
主要的:
static void Main(string[] args)
{
ListRatings lr = new ListRatings();
Rating r1 = new Rating(9, "Math", new DateTime(2021, 10, 5));
Rating r2 = new Rating(10, "sport", new DateTime(2021, 11, 3));
Rating r3 = new Rating(6, "English", new DateTime(2021, 11, 7));
lr.InsertNewRat(r1);
lr.InsertNewRat(r2);
lr.InsertNewRat(r3);
lr.PrintRats();
Console.ReadKey();
}
輸出
Math
05/10/2021 00:00:00
9
English
07/11/2021 00:00:00
6
并且程式停止并說錯誤: System.NullReferenceException [在e.getNext();我在課堂上使用的第二個ListRating]
使用此輸出,您可以看到正在列印第一個并跳轉到最后一個而不列印第二個。
我需要的輸出是
Math
05/10/2021 00:00:00
9
Sport
10
03/11/07 00:00:00
English
07/11/2021 00:00:00
6
對不起,我的英語不好
uj5u.com熱心網友回復:
您沒有正確更新您的鏈接串列。這段代碼:
else
{
first.setNext(rating);
last.setNext(rating);
}
始終將第一個和最后一個節點的下一個節點設定為您嘗試插入的那個。
相反,你想要
else
{
last.setNext(rating);
last = rating;
}
這會將當前最后一個節點設定為新節點,然后將指向最后一個節點的指標更新為評級,該節點現在應該是鏈表中的最后一個。
至于 NullReferenceException,@TheVillageIdiot 是正確的
uj5u.com熱心網友回復:
您可以調整此解決方案:
class Program
{
static void Main(string[] args)
{
var list = new List<int>();
list.Append(1);
list.Append(2);
list.Append(3);
list.Append(4);
list.Append(5);
list.Print();
var list2 = new List<int>(11, 22, 33);
list.Concat(list2);
list.Print();
}
}
public class List<T>
{
Node<T> first;
Node<T> last;
public List()
{
}
public List(params T[] data)
{
Append(data);
}
public void Append(params T [] data)
{
foreach (T d in data)
{
var n = new Node<T> { data = d };
if (last == null)
{
first = n;
last = n;
}
else
{
last.next = n;
last = n;
}
}
}
public void Concat(List<T> list)
{
if (last == null)
{
first = list.first;
last = list.last;
}
else
{
last.next = list.first;
last = list.last;
}
}
public void Print()
{
var n = first;
while (n != null)
{
Debug.Write(n.data);
if (n.next != null)
Debug.Write(" -> ");
n = n.next;
}
Debug.WriteLine("");
}
}
public class Node<T>
{
public T data { get; set; }
public Node<T> next { get; set; }
}
uj5u.com熱心網友回復:
您正在嘗試創建一個鏈表實作。不確定您是否需要last在RatingsList課堂上。就個人而言,我也會重命名first為rating。問題是您如何迭代PrintRats函式中的評級。將其更改為此,然后嘗試:
public void PrintRats()
{
Rating e = first;
while(e!=null)
{
Console.WriteLine(e.getMatter());
Console.WriteLine(e.getDate());
Console.WriteLine(e.getRate());
e = e.getNext();
}
}
此外,您可以將Console.WriteLine上述方法中的所有內容移入Rating課堂。最后,命名方法PrintRats而PrintRatings不是使其簡潔。
PS:如果我聽起來很暴躁,這還是早茶之前。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411689.html
標籤:
