none
集合问题请教~! RRS feed

  • 问题

  • 用集合操作如何取得学校下面的所有的学生?

    不用foreach如何取得学校下面的所有的学生?

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace TestJoin
    {
        class Program
        {
            static void Main(string[] args)
            {
                School s = new School();
                for (int i = 0; i < 5; i++)
                {
                    s.Classes.Add(new Class());
                }
                s.Classes[0].Students.Add(new Student(1, "a0"));
                s.Classes[1].Students.Add(new Student(1, "b0"));
                s.Classes[2].Students.Add(new Student(0, "c0"));
                s.Classes[3].Students.Add(new Student(0, "d0"));
                s.Classes[4].Students.Add(new Student(0, "e0"));

                s.Classes[0].Students.Add(new Student(0, "a1"));
                s.Classes[0].Students.Add(new Student(1, "a1"));
                s.Classes[0].Students.Add(new Student(1, "a2"));
                s.Classes[0].Students.Add(new Student(1, "a3"));
                s.Classes[1].Students.Add(new Student(0, "b1"));
                s.Classes[2].Students.Add(new Student(0, "c1"));
                s.Classes[3].Students.Add(new Student(0, "d1"));

                //如何取出school下的所有的student
                Console.ReadKey();
            }
        }
        class School
        {
            private  IList<Class> m_Classes = new List<Class> ();
            internal IList<Class> Classes
            {
              get { return m_Classes; }
              set { m_Classes = value; }
            }
        }
        class Class
        {
            private IList<Student> m_Students = new List<Student>();
            internal IList<Student> Students
            {
                get { return m_Students; }
                set { m_Students = value; }
            }
        }
        class Student
        {
            public Student(int i,string name)
            {
                m_Sex = i;
                m_Name = name;
            }

            private string m_Name;

            public string Name
            {
                get { return m_Name; }
                set { m_Name = value; }
            }

            private int m_Sex;

            public int Sex
            {
                get { return m_Sex; }
                set { m_Sex = value; }
            }
        }
    }

    2008年7月30日 3:35

答案

  •  

    for (int i = 0; i < s.Classes.Count; i++)
                {
                    for (int j = 0; j < s.ClassesIdea.Students.Count; j++)
                    {
                        Console.WriteLine(s.ClassesIdea.Students[j].Name);
                    }
                }
    2008年7月30日 17:06