c#集合小记

正在揣摩《asp.net入门经典-c#编程篇》,这书看起来还行,look到集合了!需要摘些代码!自己没事看看了!--嗨!就是作个笔记!开始。。

class1
using System;
using System.Collections;

namespace yjtestcon
{
 class Class1
 {
  static void Main(string [] args)
  {
   Console.WriteLine("Create an Array typecollection of Animal" +"objects and use it:");

   //animalArray是个Animal [] 数组

   Animal[] animalArray=new Animal[2];
   int[] a=new int[1]{9};
   Console.WriteLine("*********{0}*****",a.ToString());

   //数组的添加
   Cow myCow1=new Cow("Deirder");
   animalArray[0]=myCow1;
   animalArray[1]=new Chicken("Ken");

   //数组还可以重写
   //animalArray[0]=new Cow*("Alma");

   foreach(Animal myAnimal in animalArray)
   {
    Console.WriteLine("New {0} object added to Array collection,"+
     "Name={1}",myAnimal.ToString(),myAnimal.Name);
   
   }
   Console.WriteLine("Array collection contains{0}objects.",
    animalArray.Length);//输出数组的项目个数
   animalArray[0].Feed();//数组是强类型化的, 可以直接访问自己所包含的项目类型,可以直接调用项目的方法
   ((Chicken)animalArray[1]).LayEgg();
   //由于数组的类型是抽象类Animal,因而不能访问派生类所提供的方法.而必须使用数据类型转换!
   Console.WriteLine();
   Console.WriteLine("Create an ArrayList type collection of Animal"+
    "objects and use it:");

   //animalArrayList是个ArrayList集合
   ArrayList animalArrayList=new ArrayList();

   //animalArrayList集合的添加

   Cow myCow2=new Cow("Hayley");
   animalArrayList.Add(myCow2);
   animalArrayList.Add(new Chicken("Roy"));

   foreach(Animal myAnimal in animalArrayList)
   {


    Console.WriteLine("New {0} object added to ArrayList collection,"+
     "Name={1}",myAnimal.ToString(),myAnimal.Name);
   }

   Console.WriteLine("ArrayList collection contains{0}objects.",
    animalArrayList.Count);//输出ArrayList集合的项目个数  用到了  ArrayList.count


             //ArrayList集合是System。object对象的集合(通过多态性赋给Animal对象),所以必须用类型转换!
   ((Animal)animalArrayList[0]).Feed();

   ((Chicken)animalArrayList[1]).LayEgg();
   Console.WriteLine();

   
   Console.WriteLine("Additional manipulation of ArrayList:");

  //删除myCow2项目 也可以用:animalArrayList.Remove(myCow2)
   animalArrayList.RemoveAt(0);
   
   //由于myCow2被删除了,集合里只有Chicken对象,所以可以用下面的语句访问它
   ((Animal)animalArrayList[0]).Feed();//由于只有myCow2被删除所以arrayList

   //集合可以用AddRenge()方法一次添加好几个项目.这个方法接受带有ICollection接口的任何对象,包括前面代码创建的animalArray数组
   //这个方法专用ArrayList类
   animalArrayList.AddRange(animalArray);//这样就添加了两个项目(数组有两个对象)

   //可以访问集合中的第三个对象了◎
   ((Chicken)animalArrayList[2]).LayEgg();

   foreach(Animal myAnimal in animalArrayList)
   {


    Console.WriteLine("New {0} object added to ArrayList collection,"+
     "Name={1}",myAnimal.ToString(),myAnimal.Name);
   }

   Console.WriteLine("the animal coalled {0} is at index {1}.",
    myCow1.Name,animalArrayList.IndexOf(myCow1));
   myCow1.Name="Janice";
   Console.WriteLine("The animal is now called {0}.",
    ((Animal)animalArrayList[1]).Name);
   Console.Read();
   }
  }
 
   }
               

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值