在不使用列表方法或库的情况下创建对象列表。 C#
create list of objects without using list method or library. c#
我的情况
- 仅使用 classes
创建对象列表
代码是这样工作的
- 使用 class
ListRatings
我可以创建 Rating
的列表(不使用 c# 库 System.Collections.Generic 提供的列表方法)
(你可以在下面的代码中看到什么属性和方法有class ListRatings
和class Rating
)
问题
当我尝试打印我添加到列表中的所有评级时,我的程序只打印第一个和最后一个!
我的代码
class 评分:
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());
}
}
class 评分:
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();
我在 class 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
抱歉我的英语不好
您正在尝试创建链表实现。不确定 RatingsList
class 中是否需要 last
。就个人而言,我也会将 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
class中。最后,命名方法 PrintRats
而不是 PrintRatings
并不能使它简洁。
PS:如果我听起来胡思乱想,这还是早茶之前
您可以调整此解决方案:
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; }
}
您没有正确更新链接列表。此代码:
else
{
first.setNext(rating);
last.setNext(rating);
}
始终将第一个和最后一个节点的下一个节点设置为您要插入的节点。
相反,您想要
else
{
last.setNext(rating);
last = rating;
}
这会将当前最后一个节点设置为新节点,然后将指向最后一个节点的指针更新为评级,它现在应该是链表中的最后一个。
至于 NullReferenceException,@TheVillageIdiot 是正确的
我的情况
- 仅使用 classes 创建对象列表
代码是这样工作的
- 使用 class
ListRatings
我可以创建Rating
的列表(不使用 c# 库 System.Collections.Generic 提供的列表方法)
(你可以在下面的代码中看到什么属性和方法有class ListRatings
和class Rating
)
问题
当我尝试打印我添加到列表中的所有评级时,我的程序只打印第一个和最后一个!
我的代码
class 评分:
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());
}
}
class 评分:
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();
我在 class 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
抱歉我的英语不好
您正在尝试创建链表实现。不确定 RatingsList
class 中是否需要 last
。就个人而言,我也会将 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
class中。最后,命名方法 PrintRats
而不是 PrintRatings
并不能使它简洁。
PS:如果我听起来胡思乱想,这还是早茶之前
您可以调整此解决方案:
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; }
}
您没有正确更新链接列表。此代码:
else
{
first.setNext(rating);
last.setNext(rating);
}
始终将第一个和最后一个节点的下一个节点设置为您要插入的节点。
相反,您想要
else
{
last.setNext(rating);
last = rating;
}
这会将当前最后一个节点设置为新节点,然后将指向最后一个节点的指针更新为评级,它现在应该是链表中的最后一个。
至于 NullReferenceException,@TheVillageIdiot 是正确的