java 我应该将 ArrayLists 声明/初始化为 <Cat> 的 Lists、ArrayLists 或 ArrayLists
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5488212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Should I declare/Initialize ArrayLists as Lists, ArrayLists, or ArrayLists of <Cat>
提问by davidahines
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>();
}
}
回答by Matt Ball
You should declare it as a List<Cat>
, and initialize it as an ArrayList<Cat>
.
您应该将其声明为 a List<Cat>
,并将其初始化为ArrayList<Cat>
。
List
is an interface, and ArrayList
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.
List
是一个接口,ArrayList
是一个实现类。几乎总是最好针对接口而不是实现进行编码。这样,如果您以后需要更改实现,就不会破坏针对接口编码的使用者。
Depending on how you actually use the list, you might even be able to use the less-specific java.util.Collection
(an interface which List
extends).
根据您实际使用列表的方式,您甚至可以使用不太具体的java.util.Collection
(List
扩展的接口)。
As for List<Cat>
(you can read that as "list of cat") vs List
: that's Java's generics, which ensure compile-time type safely. In short, it lets the compiler make sure that the List
only contains Cat
objects.
至于List<Cat>
(您可以将其读作“猫列表”)vs List
:那是 Java 的泛型,它确保编译时类型安全。简而言之,它让编译器确保List
只包含Cat
对象。
public class CatHerder{
private final List<Cat> cats;
public CatHerder(){
this.cats = new ArrayList<Cat>();
}
}
回答by Peter Lawrey
I would do the following.
我会做以下事情。
public class CatHerder{
private final List<Cat> cats = new ArrayList<Cat>();
}
回答by f1sh
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 even List<? extends Cat>
正如马特已经说过的,使用最常见的接口/超类是最好的方法。确保始终声明出现在您的列表中的类型,因此使其成为一个List<Cat>
或什至List<? extends Cat>
If, at some later point, you want to replace the ArrayList
with, say, a LinkedList
, you won't have to change the declaration, but only the instantiation.
如果在以后的某个时候,你要替换ArrayList
用,比如说,一个LinkedList
,你不会有改变的声明,但只有实例。
回答by u290629
List
is more flexible than ArrayList
, List<Cat>
is safer than List
. so List<Cat>
is good choice.
List
更灵活的比ArrayList
,List<Cat>
是不是更安全List
。所以List<Cat>
是不错的选择。
回答by Buhake Sindi
First of all, List
is an interface and ArrayList
is an implementation of the List
interface (actually, it subclasses AbstractList
and implements List
). Therefore List cats = new ArrayList()
is valid since ArrayList
is-aList
.
首先,List
是一个接口,是接口ArrayList
的一个实现List
(实际上,它是AbstractList
和的子类implements List
)。因此List cats = new ArrayList()
有效,因为ArrayList
is-aList
。
For this:
为了这:
private List cats;
cats
becomes a raw-type (there is no reference to the Generic Type for List
), it hasn't been parameterised.
cats
成为原始类型(没有对 Generic Type 的引用List
),它没有被参数化。
Your 3rd solution is correct (it solves your problem for option 1),
您的第三个解决方案是正确的(它解决了您的选项 1 的问题),
private ArrayList<Cat> cats;
you have bounded a Generic Type E
for List<E>
to a type Cat
. Therefore, your instantiation of cats
is valid as the generic bounding is the same.
您已将 Generic Type E
for绑定List<E>
到 type Cat
。因此,您的实例化cats
是有效的,因为通用边界是相同的。
Your 2nd solution allows that only ArrayList
of cats
can be instantiated. The other 2 options allows you to instantiate any object that is-aList
, e.g. LinkedList
.
你的第二个解决方案允许只ArrayList
的cats
可以被实例化。其他 2 个选项允许您实例化任何is-a对象List
,例如LinkedList
.
回答by thkala
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.
为了确保类型安全,并且因为当前的 Java 编译器会抱怨泛型类型没有类型参数,所以您应该始终明确指定类型 - 或者<?>
如果您真的不关心。
That said, unless you use something specific to the ArrayList
class, you should use List<Cat>
to avoid tying your code to a particular List
implementation.
也就是说,除非您使用特定于ArrayList
类的东西,否则您应该使用List<Cat>
以避免将您的代码绑定到特定的List
实现。