我应该将 ArrayList 声明/初始化为列表、ArrayList 或的 ArrayList吗?
声明一个集合有什么区别
public class CatHerder{
private List cats;
public CatHerder(){
this.cats = new ArrayList<Cat>();
}
}
//or
public class CatHerder{
private ArrayList cats;
public CatHerder(){
this.cats = new ArrayList();
}
}
//or
public class CatHerder{
private ArrayList<Cat> cats;
public CatHerder(){
this.cats = new ArrayList<Cat>();
}
}
What is the difference in declaring a collection as such
public class CatHerder{
private List cats;
public CatHerder(){
this.cats = new ArrayList<Cat>();
}
}
//or
public class CatHerder{
private ArrayList cats;
public CatHerder(){
this.cats = new ArrayList();
}
}
//or
public class CatHerder{
private ArrayList<Cat> cats;
public CatHerder(){
this.cats = new ArrayList<Cat>();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您应该将其声明为
List
,并将其初始化为ArrayList
。List
是一个接口,< a href="http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html">ArrayList
是一个实现类。几乎总是最好针对接口而不是实现进行编码。这样,如果您稍后需要更改实现,也不会破坏针对接口进行编码的消费者。根据您实际使用该列表的方式,您甚至可以使用不太具体的
java.util.Collection
(List
扩展的接口)。至于
List
(您可以将其读作“猫列表”)与List
:这是 Java 的 泛型,确保编译时类型安全。简而言之,它让编译器确保List
仅包含Cat
对象。You should declare it as a
List<Cat>
, and initialize it as anArrayList<Cat>
.List
is an interface, andArrayList
is an implementing class. It's almost always preferable to code against the interface and not the implementation. This way, if you need to change the implementation later, it won't break consumers who code against the interface.Depending on how you actually use the list, you might even be able to use the less-specific
java.util.Collection
(an interface whichList
extends).As for
List<Cat>
(you can read that as "list of cat") vsList
: that's Java's generics, which ensure compile-time type safely. In short, it lets the compiler make sure that theList
only containsCat
objects.我会做以下事情。
I would do the following.
为了确保类型安全,并且由于当前的 Java 编译器会抱怨泛型类型没有类型参数,因此您应该始终显式指定类型 - 或者
(如果您确实不这样做)关心。
也就是说,除非您使用
ArrayList
类特有的内容,否则您应该使用List
以避免将代码绑定到特定的List
执行。In order to ensure type safety, and because current Java compilers will complain if a generic type has no type argument, you should always specify a type explicitly - or
<?>
if you really don't care.That said, unless you use something specific to the
ArrayList
class, you should useList<Cat>
to avoid tying your code to a particularList
implementation.正如马特已经说过的,使用最常见的接口/超类是最好的方法。
确保始终声明列表中出现的类型,因此将其设为
List
甚至List
如果稍后您想用
LinkedList
等替换ArrayList
,则无需更改声明,但只是实例化。As Matt already stated, using the most common Interface/Superclass is the best way to go here.
Make sure to always declare the Type that appears in your List, so make it a
List<Cat>
or evenList<? extends Cat>
If, at some later point, you want to replace the
ArrayList
with, say, aLinkedList
, you won't have to change the declaration, but only the instantiation.List
比ArrayList
更灵活,List
比List
更安全。所以List
是不错的选择。List
is more flexible thanArrayList
,List<Cat>
is safer thanList
. soList<Cat>
is good choice.首先,
List
是一个接口,ArrayList
是List
接口的实现(实际上,它是AbstractList
的子类> 和实现列表
)。因此,List cats = new ArrayList()
是有效的,因为ArrayList
is-aList
。为此:
cats
成为原始类型(没有对List
的通用类型的引用),它尚未被参数化。您的第三个解决方案是正确的(它解决了选项 1 的问题),
您已将
List
的通用类型E
绑定到类型Cat.因此,您的
cats
实例化是有效的,因为通用边界是相同的。您的第二个解决方案允许仅实例化
cats
的ArrayList
。其他 2 个选项允许您实例化任何是List
的对象,例如LinkedList
。First of all,
List
is an interface andArrayList
is an implementation of theList
interface (actually, it subclassesAbstractList
andimplements List
). ThereforeList cats = new ArrayList()
is valid sinceArrayList
is-aList
.For this:
cats
becomes a raw-type (there is no reference to the Generic Type forList
), it hasn't been parameterised.Your 3rd solution is correct (it solves your problem for option 1),
you have bounded a Generic Type
E
forList<E>
to a typeCat
. Therefore, your instantiation ofcats
is valid as the generic bounding is the same.Your 2nd solution allows that only
ArrayList
ofcats
can be instantiated. The other 2 options allows you to instantiate any object that is-aList
, e.g.LinkedList
.