Array与ArrayList

简介:

代码图理解复杂代码
422101-20170428164552959-479536684.png

类图
422101-20170428164710694-2007788377.png

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

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


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6782323.html,如需转载请自行联系原作者


相关文章
|
8月前
|
存储 Java 索引
【面试题精讲】ArrayList 和 Array(数组)的区别?
【面试题精讲】ArrayList 和 Array(数组)的区别?
|
10月前
|
Java
Java 数组(Array)与集合(List、ArrayList ...)的区别
Java 数组(Array)与集合(List、ArrayList ...)的区别
110 0
Java 最常见面试题:Array 和 ArrayList 有何区别?
Java 最常见面试题:Array 和 ArrayList 有何区别?
|
1月前
|
Python
使用array()函数创建数组
使用array()函数创建数组。
29 3
|
1月前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
38 3
|
1月前
|
JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(下)
一个数组的元素可以是另外一个数组,这样就构成了多维数组(Multi-dimensional Array)。
|
1月前
|
存储 JavaScript 前端开发
总结TypeScript 的一些知识点:TypeScript Array(数组)(上)
数组对象是使用单独的变量名来存储一系列的值。
|
1月前
|
Ruby
|
5天前
|
存储 安全 算法
C++的内置数组和STL array、STL vector
C++的内置数组和STL array、STL vector
|
1月前
|
JavaScript 前端开发 索引
在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
【4月更文挑战第16天】在JavaScript中,可以使用数组字面量或Array构造函数来创建一个数组对象
31 4