class Student
{
       public Student(string name, int age)
       { 
             Name = name; 
             Age = age;
       }
       public string Name;
       public int Age;
}
static void Main(string[] args)
{
       ArrayList students = new ArrayList();       Student scofield = new Student("Scofield", 28);
       Student zhang = new Student("张靓靓", 20);
       Student jay = new Student("周杰杰", 21);
       students.Add(scofield);
       students.Add(zhang);
       students.Add(jay);       //为什么往集合中加入具有相同属性值的对象,不会被加入到集合中?难道系统
       //能够智能判断它与集合中的某个元素的值是一样的?stu2不是在一块新内存空间上吗?
       //那么向集合中加入对象,不是将该内存区的引用给了集合吗?那么集合对象不是应该有四个了吗?
       Student stu2 = new Student("Scofield", 28);       //为什么定义了一个与前面相同的对象加入进去之后,集合中还是只有三个对象?
       //集合到底是一个什么概念?它在内存中,系统是如何调用的,是如何展示的?
       Console.WriteLine(students.Count);
       //在这里我通过对象引用来删除对象,却可以删除stu2,但是却不可以删除和它具有相同属性值
       //的对象scofield,既然将stu2加入集合的时候,集合元素个数没有增加,那么为什么删除
       //的时候,同值的元素不会被删除?集合同数组有什么区别呢?
       students.Remove(stu2);
       Console.WriteLine(students.Count);
}

解决方案 »

  1.   

    楼主什么时候往集合里面加的stu2?
      

  2.   

    首先 我没发现你把stu2加进students里面,怎么就remove?
      

  3.   

    using System;
    using System.Collections;class Student
    {
      public Student(string name, int age)
      { 
        Name = name; 
        Age = age;
      }
      public string Name;
      public int Age;  static void Main()
      {
        ArrayList students = new ArrayList();    Student scofield = new Student("Scofield", 28);
        Student zhang    = new Student("张靓靓", 20);
        Student jay      = new Student("周杰杰", 21);    students.Add(scofield);
        students.Add(zhang);
        students.Add(jay);    Student stu2 = new Student("Scofield", 28);
        students.Add(stu2); // <------------------- 你漏写了这一句!
        
        Console.WriteLine(students.Count); // 输出:4    students.Remove(stu2);
        Console.WriteLine(students.Count); // 输出:3
      }
    }
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Collections;namespace CSharpTest
    {
        class Student
        {
            public Student(string name, int age)
            {
                Name = name;
                Age = age;
            }
            public string Name;
            public int Age;
        }    class Program
        {        static void Main(string[] args)
            {
                ArrayList students = new ArrayList();            Student scofield = new Student("Scofield", 28);
                Student zhang = new Student("张靓靓", 20);
                Student jay = new Student("周杰杰", 21);
                students.Add(scofield);
                students.Add(zhang);
                students.Add(jay);
                foreach(Student s in students )
                    Console.WriteLine(s.Name);
               
                Student stu2 = new Student("Scofield", 28);
                Console.WriteLine("---------------------");
                Console.WriteLine(students.Count);
                foreach (Student s in students)
                    Console.WriteLine(s.Name);
                Console.WriteLine("---------------------");
                students.Remove(stu2);
                foreach (Student s in students)
                    Console.WriteLine(s.Name);
                Console.WriteLine(students.Count);
                Console.WriteLine("---------------------");           
            }
        }
    }Scofield
    张靓靓
    周杰杰
    ---------------------
    3
    Scofield
    张靓靓
    周杰杰
    ---------------------
    Scofield
    张靓靓
    周杰杰
    3
    ---------------------
    请按任意键继续. . .
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Collections;namespace CSharpTest
    {
        class Student
        {
            public Student(string name, int age)
            {
                Name = name;
                Age = age;
            }
            public string Name;
            public int Age;
        }    class Program
        {        static void Main(string[] args)
            {
                ArrayList students = new ArrayList();            Student scofield = new Student("Scofield", 28);
                Student zhang = new Student("张靓靓", 20);
                Student jay = new Student("周杰杰", 21);
                students.Add(scofield);
                students.Add(zhang);
                students.Add(jay);
                foreach(Student s in students )
                    Console.WriteLine(s.Name);
               
                Student stu2 = new Student("Scofield", 28);
                students.Add(stu2);
                Console.WriteLine("---------------------");
                Console.WriteLine(students.Count);
                foreach (Student s in students)
                    Console.WriteLine(s.Name);
                Console.WriteLine("---------------------");
                students.Remove(stu2);
                foreach (Student s in students)
                    Console.WriteLine(s.Name);
                Console.WriteLine(students.Count);
                Console.WriteLine("---------------------");           
            }
        }
    }Scofield
    张靓靓
    周杰杰
    ---------------------
    4
    Scofield
    张靓靓
    周杰杰
    Scofield
    ---------------------
    Scofield
    张靓靓
    周杰杰
    3
    ---------------------
    请按任意键继续. . .
    看到区别没?
      

  6.   

    不好意思,忘记加那条代码了,是在stu2创建之后就添加进集合中了。即
    Student stu2 = new Student("Scofield", 28);
    students.Add(stu2);