这个带有List和ArrayList的语句是什么意思? [英] What does this statement with List and ArrayList mean?

查看:44
本文介绍了这个带有List和ArrayList的语句是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Drools Planner示例,并且碰到了很多这样的代码:

I am reading the Drools Planner examples and I came across code like this a lot:

List<Column> columnList = new ArrayList<Column>(n);

据我了解,它应该初始化一个长度为n的列表,该列表存储 Column 数据类型.

As far as I get it, it is supposed to initialize a list of length n which stores the Column datatype.

但是在表达式的两边具有不同的集合数据类型怎么办?

But what is the deal with having different collection datatypes on either side of the expression?

如果 ArrayList< Column> List< Column> 类型匹配,则与执行以下操作有何不同:

If ArrayList<Column> type-matches with List<Column>, what makes it different from doing:

List<Column> columnList = new List<Column>(n);

推荐答案

List 是一个接口.您无法创建接口实例.

List is an interface. You can't create instances of interfaces.

ArrayList 是实现List的类,您可以创建一个.

ArrayList is a class which implements List, you can create one.

接口仅定义行为.实现并实现该行为的类.

An interface just defines behavior. A class that implements and interface implements that behavior.

您会注意到,如果查看API,,许多不同的类都实现了 List 接口.这是因为它们都提供了 List 定义的方法的实现.而且,这些实现可能大不相同,因为 ArrayList 的工作方式不同于 LinkedList .

You will note that if you look at the API, many different classes implement the List interface. That's because they all provide implementations for the methods that List defines. Moreover, those implementations are likely mostly different, because and ArrayList works differently than, say, a LinkedList.

最好使用表格

列表列表=新的ListType()

因为您以后可以更改列表实现,而又不影响代码.原因是如果您这样做

because you can change the list implementation later, if you want to, without affecting your code. The reason for this is if you do

ListType list = new ListType()

列表类型是 ListType ,只有 ListType 和子类的一个实例.

the type of list is ListType, which only an instance of ListType and subclasses have.

如果执行上面显示的首选分配,则可以将实现 List 任何内容分配给 list .

If you do the preferred assignment shown above, then you can assign anything that implements List to list.

这篇关于这个带有List和ArrayList的语句是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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