List 和 ArrayList的区别

List是Collection的一个子接口,是一个有序的集合!

ArrayList是List的一个实现类!、

阅读:

List 和 ArrayList 的区别_蔡小波的博客-CSDN博客_list和arraylist

List<String> list=new ArrayList<String>();

声明为List而不是 ArrayList,使得List这个对象有多种储存形势,不仅仅局限于Arraylist

List list1 = new List(); ------这么定义就是错误的,因为他是一个接口,不能作为一个实例对象,只可以作为一个引用!

List list2 = new ArrayList();//

用List作为对象引用,只能调用ArrayList中与List的公共属性和方法;

ArrayList list3 = new ArrayList();//

而用ArrayList作为对象引用当然就可以调用其中所有方法及属性。


一般情况下如果是用到了第二种定义方法,就说明一定是用到了ArrayList特有的方法或属性。
同时如果List与ArrayList中有相同的属性(如:int i)以及相同的方法(如:void f()),那么当当使用第一种定义方法时,list2.i调用的就是List中的i,list2.f()调用的就是ArrayList中的f().

List和ArrayList区别及用法_hardenyeahyeah的博客-CSDN博客_arraylist list

这个回答也很棒:

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

java - Polymorphism: Why use "List list = new ArrayList" instead of "ArrayList list = new ArrayList"? - Stack Overflow

可以帮助你在大型项目中更改list的方向

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ArrayListListJava编程语言中的两个概念。ArrayListList接口的一个实现类,也就是说ArrayList继承了List接口并实现了其中的法。因此,我们可以说ArrayList是一个特定类型的List。 使用List list = new ArrayList()的写法,是利用了Java中的多态性。多态性允许我们将一个子类的对象赋值给一个父类的引用变量。在这种情况下,我们把ArrayList对象赋值给了List类型的引用变量list。这样做的好处是,我们可以灵活地使用List接口中定义的法,而不仅仅局限于ArrayList法。这样一来,如果我们在其他地需要使用List接口的特性时,可以便地替换ArrayList为其他实现了List接口的类。 另一面,使用ArrayList list = new ArrayList()的写法,创建了一个具体类型为ArrayList的对象。这种式保留了ArrayList的所有属性和法,但是在某些情况下可能会限制了代码的灵活性。 综上所述,使用List list = new ArrayList()的写法更加推荐,因为它允许我们利用多态性,使得list对象能够适应不同的储存形式,而不仅仅局限于ArrayList。同时,这样做也符合面向接口编程的原则,增加了代码的可扩展性和可维护性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ArrayListList本质上和使用上的区别详解](https://blog.csdn.net/XVJINHUA954/article/details/106437479)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ListArrayList区别](https://blog.csdn.net/jjjjjfe/article/details/125886722)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值