why using List list = new ArrayList() better than Arraylist list?

This is called programming to interface. This will be helpful in case if you wish to move to some other implementation of List in the future. If you want some methods in ArrayList then you would need to program to the implementation that is ArrayList a = new ArrayList().

The main reason you‘d do this is to decouple your code from a specific implementation of the interface. When you write your code like this:

List list = new ArrayList();

the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.

For instance, say you were writing a fairly large 3rd party library, and say that you decided to implement the core of your library with a LinkedList. If your library relies heavily on accessing elements in these lists, then eventually you‘ll find that you‘ve made a poor design decision; you‘ll realize that you should have used an ArrayList (which gives O(1) access time) instead of a LinkedList (which gives O(n) access time). Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,

List list = new LinkedList();

to

List list = new ArrayList();

and you know that this will work because you have written your code to follow the contract provided by the List interface.

On the other hand, if you had implemented the core of your library using LinkedList list = new LinkedList(), making such a change wouldn‘t be as easy, as there is no guarantee that the rest of your code doesn‘t make use of methods specific to the LinkedList class.

All in all, the choice is simply a matter of design... but this kind of design is very important (especially when working on large projects), as it will allow you to make implementation-specific changes later without breaking existing code.

why using List list = new ArrayList() better than Arraylist list?,布布扣,bubuko.com

时间: 08-03

why using List list = new ArrayList() better than Arraylist list?的相关文章

List list = new ArrayList()和ArrayList list = new ArrayList()的区别?

第一种定义,是很好的面向接口编程的习惯.比如你有这么一个方法供别人调用:public List getXXXList(){    List list = new ArrayList();    ...    return list;}现在是new ArrayList(),如果万一以后要改成new LinkedList()的话,你这个方法的返回类型就不需要改了,并且调用这个方法的所有类都不需要修改,这样就降低了修改程序的成本和风险,提高了程序的可复用性.这就是面向接口编程的好处. java的多态,

ArrayList、string、string[]之间的转换

1.ArrarList 转换为 string[] : ArrayList list = new ArrayList(); list.Add("aaa"); list.Add("bbb"); string[] arrString = (string[])list.ToArray(typeof( string)) ; 2.string[] 转换为 ArrarList : ArrayList list = new ArrayList(new string[] { &quo

ArrayList&LinkedList&Map&Arrays

Java集合框架 1:集合接口 1.1:Collection接口 Collection接口是构造集合框架的基础.它声明所有类集合都将拥有的核心方法 Boolean add(Object obj) 将obj加入到调用类集合中,加入返回true 否则 返回 false Boolean addAll(Collection c) 将c中所有元素都加入到类集合中,都加入返回true否则 false Void clean() 从调用类集合中删除所有元素 Boolean contains(Object obj

蓝鸥Unity开发基础二——课时22 ArrayList

一.ArrayList 集合:集合是种容器,在程序中,使用集合管理相关对象组 集合分为非泛型集合和泛型集合 推荐视频讲师博客:http://11165165.blog.51cto.com/ 二.非泛型集合:使用非泛型集合需要引起命名空间System.Collections ArrayList--可以根据需要动态增加的数组 Hashtable--用来存储键值对的哈希表 Queue--遵循先进先出的对列 Stack--遵循后进先出的栈 三.泛型集合:使用泛型集合需要引入命名空间System.Coll

C#中数组、ArrayList和List三者的区别

在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到底有什么样的区别呢. 数组 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. [csharp] view plaincopy <span style="font-family:SimSun;font-size:18px;">//数组 string[] s=new string[2]; //赋值 s[0]="a"; s[1]=&quo

C#基础[10] ArrayList集合(二)【详细介绍】

1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和IList接口 灵活的设置数组的大小 2.如何使用ArrayList 最简单的例子: ArrayList List = new ArrayList(); for( int i=0;i<10;i++ ) //给数组增加10个Int元素 List.Add(i); //..程序做一些处理 List.RemoveA

C#基础[9] ArrayList集合(一)

集合可以看做数组-> 创建一个长度为10的数组    数组长度为10,要插入新的数据,就得重新创建数组 排序-> 长度固定,不太灵活 使用集合即可方便解决这些问题可以将集合看作为"长度可变的,具有很多方法的数组" using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Col

ArrayList类的用法

1.什么是ArrayList ArrayList就是传说中的动态数组,是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和IList接口 灵活的设置数组的大小 2.如何使用ArrayList 最简单的例子: ArrayList List = new ArrayList(); for( int i=0;i <10;i++ ) //给数组增加10个Int元素 List.Add(i); //..程序做一些处理 List.RemoveAt(5);//将第6

List、ArrayList、LinkedList的区别及使用

首先我们要知道List是java中的接口,而不是实现类,所以它是不能实例化的,例如以下代码: 1 public static void main(String[] args) { 2 List list=new List(); 3 4 } java中会报错,而ArrayList和LinkedList是实现了这个接口的实现类,可以进行实例化,其定义如下: 1 public static void main(String[] args) { 2 3 ArrayList list1=new Array