我正在练习ArrayList,与此同时进行堆栈收集
namespace Practice
{
public class Student
{
public static Stack Grades = new Stack();
public string FirstName { get; set; }
public string LastName { get; set; }
public Student(string firstname, string lastname)
{
this.FirstName = firstname;
this.LastName = lastname;
}
public void grades(params int[] score)
{
Grades.Push(score);
}
}
}
namespace Practice
{
class Program
{
static void Main(string[] args)
{
ArrayList StuArray = new ArrayList();
Student stu1 = new Student("Super", "Geek");
Student stu2 = new Student("Jack", "Nerd");
Student stu3 = new Student("kim", "meh");
stu1.grades(10, 20, 30, 40, 50);
stu2.grades(29, 24, 54, 75, 32);
stu3.grades(21, 23, 55, 99, 24);
StuArray.Add(stu1);
StuArray.Add(stu2);
StuArray.Add(stu3);
foreach (Student student in StuArray)
{
Console.WriteLine($"The student First Name is {student.FirstName} and Last Name is {student.LastName}");
}
}
}
}
即使我通过“成绩”中的“成绩”方法通过“学生”对象将数据存储在“成绩”堆栈中,在这种情况下,我仍然无法显示堆栈。
我以为
foreach ( object a in stu1.grades)
{
Console.WriteLine($"the grades of each student is {a}");
}
之类的,但是我错了。
有人可以给我任何提示吗?
谢谢〜!
答案 0 :(得分:0)
我的第一个猜测是您要将整个param集合添加到堆栈的第一项,而不是单个项。
以下代码应该可以使用。
public void grades(params int[] score)
{
foreach(var grade in score)
{
Grades.Push(score);
}
}