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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:21:35  来源:igfitidea点击:

Should I declare/Initialize ArrayLists as Lists, ArrayLists, or ArrayLists of <Cat>

javalistcollectionsarraylist

提问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>

Listis an interface, and ArrayListis 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 Listextends).

根据您实际使用列表的方式,您甚至可以使用不太具体的java.util.CollectionList扩展的接口)。

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 Listonly contains Catobjects.

至于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 ArrayListwith, say, a LinkedList, you won't have to change the declaration, but only the instantiation.

如果在以后的某个时候,你要替换ArrayList用,比如说,一个LinkedList,你不会有改变的声明,但只有实例。

回答by u290629

Listis more flexible than ArrayList, List<Cat>is safer than List. so List<Cat>is good choice.

List更灵活的比ArrayListList<Cat>是不是更安全List。所以List<Cat>是不错的选择。

回答by Buhake Sindi

First of all, Listis an interface and ArrayListis an implementation of the Listinterface (actually, it subclasses AbstractListand implements List). Therefore List cats = new ArrayList()is valid since ArrayListis-aList.

首先,List是一个接口,是接口ArrayList的一个实现List(实际上,它是AbstractList和的子类implements List)。因此List cats = new ArrayList()有效,因为ArrayListis-aList

For this:

为了这:

private List cats;

catsbecomes 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 Efor List<E>to a type Cat. Therefore, your instantiation of catsis valid as the generic bounding is the same.

您已将 Generic Type Efor绑定List<E>到 type Cat。因此,您的实例化cats是有效的,因为通用边界是相同的。

Your 2nd solution allows that only ArrayListof catscan be instantiated. The other 2 options allows you to instantiate any object that is-aList, e.g. LinkedList.

你的第二个解决方案允许只ArrayListcats可以被实例化。其他 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 ArrayListclass, you should use List<Cat>to avoid tying your code to a particular Listimplementation.

也就是说,除非您使用特定于ArrayList类的东西,否则您应该使用List<Cat>以避免将您的代码绑定到特定的List实现。