在Java中的ArrayList或列表声明 [英] ArrayList or List declaration in Java

查看:137
本文介绍了在Java中的ArrayList或列表声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这两个声明之间的区别?

宣言1:

 的ArrayList<串GT; ArrayList的=新的ArrayList<串GT;();

2宣言:

 列表<串GT; ArrayList的=新的ArrayList<串GT;();


解决方案

 列表<串GT; ArrayList的=新的ArrayList<串GT;();

要隐藏实现细节的同时,将其返回到客户端,在时间,你可以从 ArrayList的改变实施后的点是通用的LinkedList 透明。

这个机制是在你设计库等情况下非常有用,它可以在时间上的客户端最小的改动某些时候改变自己的实施细节。

 的ArrayList<串GT; ArrayList的=新的ArrayList<串GT;();

这强制要求你总是需要返回的ArrayList 。在一定的时间点,如果你想改变实施细则的LinkedList ,应该在客户端的变化也使用的LinkedList 而不是的ArrayList

What is the difference between these two declarations?

Declaration 1:

ArrayList<String> arrayList = new ArrayList<String>();

Declaration 2:

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

解决方案

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

Is generic where you want to hide implementation details while returning it to client, at later point of time you may change implementation from ArrayList to LinkedList transparently.

This mechanism is useful in cases where you design libraries etc., which may change their implementation details at some point of time with minimal changes on client side.

ArrayList<String> arrayList = new ArrayList<String>();

This mandates you always need to return ArrayList. At some point of time if you would like to change implementation details to LinkedList, there should be changes on client side also to use LinkedList instead of ArrayList.

这篇关于在Java中的ArrayList或列表声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆