我正在学习关于c#的MOOC课程。我们必须创建一个Arraylist
类型的学生,然后使用foreach
循环必须迭代它并打印名称。我已经尝试了所有的铸造方法,但无法通过它。请帮忙
c.students.Add(student1);
c.students.Add(student2);
c.students.Add(student3);
foreach(object o in students)
{
Student s = (Student)o;
Console.WriteLine(s.FirstName);
}
c是课程对象。当然是一堂课。学生是arraylist。学生是一个班级。
答案 0 :(得分:1)
不确定您在哪里遇到错误。看看我的.NET小提琴here。代码如下所示。希望它有所帮助。
using System;
using System.Collections;
public class Program
{
public static void Main()
{
var students = new ArrayList();
students.Add(new Student() { FirstName = "John", LastName = "Doe" });
students.Add(new Student() { FirstName = "Richard", LastName = "Roe" });
foreach(Student s in students)
{
Console.WriteLine(s.FirstName);
}
}
}
public class Student
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
答案 1 :(得分:1)
foreach(c.students中的对象o)
这应该这样做,这可能是一个愚蠢的错误
namespace stackOverflow
{
class Program
{
static void Main(string[] args)
{
course c = new course();
student student1 = new student("a");
student student2 = new student("b");
student student3 = new student("c");
c.students.Add(student1);
c.students.Add(student2);
c.students.Add(student3);
foreach (object o in c.students)
{
student s = (student)o;
Console.WriteLine(s.name);
}
}
}
class course
{
public List<student> students = new List<student>();
}
class student
{
public string name { get; set; }
public student(string s)
{
name = s;
}
}
}
答案 2 :(得分:1)
ArrayList students = new ArrayList();
此行应为:c.students = new ArrayList();
,如Channs先前所述。
正如你所写的那样,它试图在你的主要函数中创建一个名为students的 new 变量,它永远不会访问你的课程对象中的学生数组。 / p>
虽然内部对象变量的初始化应该在对象本身内完成。 所以在你的课程对象中做一些更像这样的事情: 这样,每当您声明一个类型为Course的新对象(即: Course c = new Course())时,它将自动初始化该数组。 我注意到的另一个问题是你的学生构造函数声明中,你总是试图获取字符串fname的参数。 公共学生(字符串fname) 然后在您的代码中,您永远不会传递该数据,即: 学生student1 =新学生(); 因此,要么在初始化时传递firstname变量,要么在Student中更改构造函数以允许它不接受任何内容,如下所示: 这样您就不必传递数据,但如果这样做,它将被复制到学生对象的名字。 您可以随时将null更改为“John”或“No Name”之类的内容,以便在对象中具有可打印数据。只是一个建议。 此致
Slipoch class Course
{
public ArrayList students;
public Course()
{
students = new ArrayList();
}
}
class Student
{
private string firstName;
private string lastName;
public Student(string fname = null)
{
this.FirstName = fname;
}
答案 3 :(得分:0)
这是你的代码修好了。有两件事:
希望这有帮助。
using System;
using System.Collections;
class Program
{
public static void Main(string[] args)
{
Student student1 = new Student();
student1.FirstName = "a";
student1.LastName = "w";
Student student2 = new Student();
student2.FirstName = "e";
student2.LastName = "s";
Student student3 = new Student();
student3.FirstName = "i";
student3.LastName = "o";
Course c = new Course();
ArrayList students = new ArrayList();
c.students.Add(student1);
c.students.Add(student2);
c.students.Add(student3);
foreach (Student o in c.students)
{
Student s = (Student)o;
Console.WriteLine(s.FirstName);
}
Console.ReadLine();
}
}
internal class Course
{
public ArrayList students = new ArrayList();
}
internal class Student
{
private string firstName;
private string lastName;
public Student()
{
}
public Student(string fname)
{
this.FirstName = fname;
}
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
}
}
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
}
}
}