Array与ArrayList

简介:

代码图理解复杂代码

类图

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,如需转载请自行联系原作者


相关文章
|
存储 Java 索引
【面试题精讲】ArrayList 和 Array(数组)的区别?
【面试题精讲】ArrayList 和 Array(数组)的区别?
|
存储 安全 Java
从源码到场景,用 5 分钟讲透 Array 和 ArrayList 的差异
大家好,我是小米,29岁的技术分享者。今天聊聊社招面试中常见的问题——Array和ArrayList的区别。数组是固定大小的容器,长度不可变,性能高;ArrayList是动态数组,可自动扩容,支持更多操作但性能稍逊。在实际开发中,根据需求选择:高性能、固定大小选数组;灵活操作选ArrayList。希望这篇文章能帮你答出漂亮的答案!欢迎关注我的微信公众号“软件求生”,获取更多技术干货。
306 5
ArrayList集合常用方法,.set可以用来生成图片和赋值命名,array.remove(1),array.set(1,“xxxx”)可以修改指定位置,array.size可以获取元素的个数
ArrayList集合常用方法,.set可以用来生成图片和赋值命名,array.remove(1),array.set(1,“xxxx”)可以修改指定位置,array.size可以获取元素的个数
数据存储之数组的特点,长度固定,适应变化需求,集合类特点是空间可变,ArrayList泛型,ArrayList<String> array = new ArrayList<String>()
数据存储之数组的特点,长度固定,适应变化需求,集合类特点是空间可变,ArrayList泛型,ArrayList<String> array = new ArrayList<String>()
|
Java
Java 数组(Array)与集合(List、ArrayList ...)的区别
Java 数组(Array)与集合(List、ArrayList ...)的区别
1249 0
Java 最常见面试题:Array 和 ArrayList 有何区别?
Java 最常见面试题:Array 和 ArrayList 有何区别?
|
存储 缓存 NoSQL
Java 8 ArrayList hugeCapacity 函数与 MAX_ARRAY_SIZE
Java 8 ArrayList hugeCapacity 函数与 MAX_ARRAY_SIZE
646 0
Java 8 ArrayList hugeCapacity 函数与 MAX_ARRAY_SIZE
|
测试技术 PHP 开发者
PHP 数组查找:为什么 `isset()` 比 `in_array()` 快得多?
PHP 数组查找:为什么 `isset()` 比 `in_array()` 快得多?
|
人工智能 Java
Java 中数组Array和列表List的转换
本文介绍了数组与列表之间的相互转换方法,主要包括三部分:1)使用`Collections.addAll()`方法将数组转为列表,适用于引用类型,效率较高;2)通过`new ArrayList<>()`构造器结合`Arrays.asList()`实现类似功能;3)利用JDK8的`Stream`流式计算,支持基本数据类型数组的转换。此外,还详细讲解了列表转数组的方法,如借助`Stream`实现不同类型数组间的转换,并附带代码示例与执行结果,帮助读者深入理解两种数据结构的互转技巧。
1056 1
Java 中数组Array和列表List的转换

热门文章

最新文章

  • 1
    PHP 数组查找:为什么 `isset()` 比 `in_array()` 快得多?
    322
  • 2
    Java 中数组Array和列表List的转换
    1056
  • 3
    JavaScript中通过array.map()实现数据转换、创建派生数组、异步数据流处理、复杂API请求、DOM操作、搜索和过滤等,array.map()的使用详解(附实际应用代码)
    773
  • 4
    通过array.reduce()实现数据汇总、条件筛选和映射、对象属性的扁平化、转换数据格式、聚合统计、处理树结构数据和性能优化,reduce()的使用详解(附实际应用代码)
    1607
  • 5
    通过array.some()实现权限检查、表单验证、库存管理、内容审查和数据处理;js数组元素检查的方法,some()的使用详解,array.some与array.every的区别(附实际应用代码)
    719
  • 6
    通过array.every()实现数据验证、权限检查和一致性检查;js数组元素检查的方法,every()的使用详解,array.some与array.every的区别(附实际应用代码)
    515
  • 7
    多维数组操作,不要再用遍历循环foreach了!来试试数组展平的小妙招!array.flat()用法与array.flatMap() 用法及二者差异详解
    355
  • 8
    别再用双层遍历循环来做新旧数组对比,寻找新增元素了!使用array.includes和Set来提升代码可读性
    351
  • 9
    Array.forEach实战详解:简化循环与增强代码可读性;Array.forEach怎么用;面对大量数据时怎么提高Array.forEach的性能
    233
  • 10
    深入理解 JavaScript 中的 Array.find() 方法:原理、性能优势与实用案例详解
    834