我应该将 ArrayList 声明/初始化为列表、ArrayList 或的 ArrayList吗?

发布于 2024-10-29 07:34:22 字数 445 浏览 2 评论 0原文

声明一个集合有什么区别

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水中月 2024-11-05 07:34:23

您应该将其声明为 List,并将其初始化为 ArrayList

List 是一个接口,< a href="http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html">ArrayList 是一个实现类。几乎总是最好针对接口而不是实现进行编码。这样,如果您稍后需要更改实现,也不会破坏针对接口进行编码的消费者。

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

至于 List (您可以将其读作“猫列表”)与 List:这是 Java 的 泛型,确保编译时类型安全。简而言之,它让编译器确保 List 仅包含 Cat 对象。


public class CatHerder{
    private final List<Cat> cats;
    public CatHerder(){
        this.cats = new ArrayList<Cat>();
    }
}

You should declare it as a List<Cat>, and initialize it as an 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.

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).

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.


public class CatHerder{
    private final List<Cat> cats;
    public CatHerder(){
        this.cats = new ArrayList<Cat>();
    }
}
陪我终i 2024-11-05 07:34:23

我会做以下事情。

public class CatHerder{
    private final List<Cat> cats = new ArrayList<Cat>();
}

I would do the following.

public class CatHerder{
    private final List<Cat> cats = new ArrayList<Cat>();
}
白衬杉格子梦 2024-11-05 07:34:23

为了确保类型安全,并且由于当前的 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 use List<Cat> to avoid tying your code to a particular List implementation.

回忆那么伤 2024-11-05 07:34:23

正如马特已经说过的,使用最常见的接口/超类是最好的方法。
确保始终声明列表中出现的类型,因此将其设为 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 even 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.

北凤男飞 2024-11-05 07:34:23

ListArrayList 更灵活,ListList 更安全。所以 List 是不错的选择。

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

深海少女心 2024-11-05 07:34:23

首先,List是一个接口,ArrayListList接口的实现(实际上,它是AbstractList的子类> 和实现列表)。因此,List cats = new ArrayList() 是有效的,因为 ArrayList is-a List

为此:

private List cats;

cats 成为原始类型(没有对 List 的通用类型的引用),它尚未被参数化。

您的第三个解决方案是正确的(它解决了选项 1 的问题),

private ArrayList<Cat> cats;

您已将 List 的通用类型 E 绑定到类型 Cat.因此,您的 cats 实例化是有效的,因为通用边界是相同的。

您的第二个解决方案允许仅实例化 catsArrayList 。其他 2 个选项允许您实例化任何 List 的对象,例如 LinkedList

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-a List.

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.

Your 3rd solution is correct (it solves your problem for option 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.

Your 2nd solution allows that only ArrayList of cats can be instantiated. The other 2 options allows you to instantiate any object that is-a List, e.g. LinkedList.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文