代码图理解复杂代码

类图

1.抽象动物类Animal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo2
{
public abstract class Animal
{
protected string name; public string Name
{
get
{
return name;
} set
{
name = value;
}
} public Animal()
{
name = "The animal with no name";
} public Animal(string newName)
{
name = newName;
} public void Feed()
{
Console.WriteLine("{0} has been fed.",name);
}
}
}

2.牛类Cow

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo2
{
public class Cow:Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked.",name);
} public Cow(string newName):base(newName) // 继承了父类
{ }
}
}

3.鸡类Chicken

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo2
{
public class Chicken:Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg.",name);
} public Chicken(string newName) :base(newName)
{ }
}
}

4.主类Program,用到了数组和集合Array,ArrayList

using System;
using System.Collections; // 集合
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo2
{
class Program
{
static void Main(string[] args)
{ // 数组的形式
Console.WriteLine("Create an array type collection of Animal objects and use it:");
Animal[] animalArray = new Animal[2];
Cow myCow1 = new Cow("Deirdre");
Chicken myChicken1 = new Chicken("Ken");
animalArray[0] = myCow1;
animalArray[1] = myChicken1; 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(); // 集合的形式
Console.WriteLine("Create an ArrayList type collection of Animal objects and use it:");
ArrayList animalArrayList = new ArrayList();
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("Array collection contains {0} objects.", animalArrayList.Count); // 注意这里是Count
((Animal)animalArrayList[0]).Feed();
((Chicken)animalArrayList[1]).LayEgg(); Console.ReadKey(); }
}
}

两种效果差不多,细节略有区别!

再看下面,改造

定义Animals类,不需要通过ArrayList了。Animals就是ArrayList。

Animals.cs

using System;
using System.Collections; // 集合
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo2
{
public class Animals : CollectionBase
{
public void Add(Animal newAnimal)
{
List.Add(newAnimal);
} public void Remove(Animal newAnimal)
{
List.Remove(newAnimal);
} public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex];
}
set
{
List[animalIndex] = value;
}
}
}
}

使用Animals

using System;
using System.Collections; // 集合
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Demo2
{
class Program
{
static void Main(string[] args)
{ Animals animalCollection = new Animals();
animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("Vera"));
foreach(Animal myAnimal in animalCollection)
{
myAnimal.Feed();
} Console.ReadKey(); }
}
}

Array可以包涵基本类型和对象类型,ArrayList只能包涵对象类型。

Array大小是固定的,ArrayList的大小是动态变化的。

ArrayList提供了更多的方法和特性,比如:addAll(),removeAll(),iterator()等等。

对于基本数据类型,集合使用自动装箱来减少编码的工作量。但是当处理固定大小的基本数据类型的时候这种方式相对比较慢。

方法论:

实践加理论!多查查相关的资料,总结一下!

Array与ArrayList的更多相关文章

  1. C#中数组Array、ArrayList、泛型List<T>的比较

    在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...

  2. Array和ArrayList互相转换

    class Order{ public string orderId; public string orderName; public decimal orderPrice; } public cla ...

  3. Array和ArrayList的异同点【转】

    相信数组是大家在编程最常使用的,不论任何语言都存在数组这样的数据结构,由于C#语言是完全面向对象的,所以在C#中的数组也是对象,实际上就是Array类的实例,Array类的使用可以说是使用最频繁的,只 ...

  4. Array和ArrayList的区别与联系

    博主今天去了一个java的实习面试,发现有好多java最基础的数据结构对于博主来说反而感到陌生,在面试官问一些常见的例如HashMap这样的数据结构,博主能回答的头头是道,但是在问到Array和Arr ...

  5. Array和ArrayList的异同点

    Array和ArrayList的异同点 1.不同点: (1)Array只能存储同构的对象, ArrayList可以存储异构的对象 (2)在CLR托管对中的存放方式中,Array是始终是连续存放的, A ...

  6. C# array与arraylist区别及获取sql字段名

    array与arraylist的区别: 1.  Array 的容量是固定的,而 ArrayList 的容量是根据需要自动扩展的.如果更改了 ArrayList.Capacity 属性的值,则自动进行内 ...

  7. Array和ArrayList有什么区别?

    Array和ArrayList的区别: 1.Array可以包含基本数据类型和对象类型,而ArrayList只能包含对象类型 2.Array有固定的大小,而ArrayList是动态变化的. 3.Arra ...

  8. C#数组之 []、List、Array、ArrayList应用

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. C#中[] 、List、Array、ArrayList等数据结构的差异简述

    [] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList 是针对任意类型.任意长度的. Array 和 ArrayLis ...

  10. Java面试题之Array和ArrayList的区别

    Array和ArrayList的区别: 1.Array类型的变量在声明的同时必须进行实例化(至少得初花数组的大小),而ArrayList可以只是先声明: 2.Array始终是连续存放的:而ArrayL ...

随机推荐

  1. [SDOI2010] 古代猪文 (快速幂+中国剩余定理+欧拉定理+卢卡斯定理) 解题报告

    题目链接:https://www.luogu.org/problemnew/show/P2480 题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色 ...

  2. POJ 3662 二分+Dijkstra

    题意: 思路: 二分+Disjktra 二分一个值 如果某条边的边权比它小,则连上边权为0的边,否则连上边权为1的边 最后的d[n]就是最小要免费连接多少电话线. //By SiriusRen #in ...

  3. childNodes.length和form.length的不同

    我们知道,DOM里面提供了element.childNodes.length属性,childNodes 属性返回节点的子节点集合,以 NodeList 对象. 那么childNodes包含哪些节点呢? ...

  4. IHttpHandler的学习(0-2)

    这个IHttpHandler,要想到asp.net生命周期 ,想到哪个从你发起请求开始,这个请求通过HttpModule------>IHttpHandler的: 执行HttpModule的一系 ...

  5. PostgreSQL Replication之第六章 监控您的设置(1)

    在本书的前几章,您已经学习了各种复制以及如何配额制各种类型的场景.现在是时候通过增加监控来让您的设置更加可靠了. 在本章中,您将学习监控什么以及如恶化实施合理的监控车辆.您将学习: • 检查您的 XL ...

  6. 2017国家集训队作业[agc016b]Color Hats

    2017国家集训队作业[agc016b]Color Hats 题意: 有\(N\)个人,每个人有一顶帽子.帽子有不同的颜色.现在,每个人都告诉你,他看到的所有其它人的帽子共有多少种颜色,问有没有符合所 ...

  7. NOIP2016 天天爱跑步(树上差分)

    题意 给定一棵树,从时刻 0 开始,有若干人从 S[i] 出发向 T[i] 移动,每单位时刻移动一条边 对于树上每个点 x,求 w[x]  时刻有多少人恰好路过 x N,M≤300000 题解 从上午 ...

  8. Linux下的压缩与解压缩

    1 .gz 1)压缩 root@xiaohuang-virtual-machine:/home/xiaohuang/桌面/hellow/hellow# gzip 2.txt 3.txt root@xi ...

  9. conda常用命令,如何在conda环境中安装gym库?

    查看已安装的环境: conda info -e 或 conda env list 创建新环境gymlab: conda create -n gymlab python=3.5 激活环境gymlab: ...

  10. CF17E Palisection(回文树)

    题意翻译 给定一个长度为n的小写字母串.问你有多少对相交的回文子 串(包含也算相交) . 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串 ...