Java List 和 List的区别

发布于 2024-12-11 11:51:45 字数 70 浏览 0 评论 0原文

java中ListList有什么区别?

What is the difference between List and List<Object> in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

最好是你 2024-12-18 11:51:45

假设您有两个列表:

List; mainList = new ArrayList();

和一个原始类型:

List rawList = new ArrayList();

现在让我们做这样的事情:

List<Integer> intList = new ArrayList<Integer>();
intList.add(42);
mainList.addAll(intList);
rawList.addAll(intList);

但是在某个地方还有一个列表其中包含字符串对象:

List; strList = new ArrayList()

当我们尝试调用

strList.addAll(mainList)

时,我们收到编译错误:

The method addAll(Collection) in the type List;不适用于参数 (List)。

但是,如果我们尝试调用

strList.addAll(rawList)

我们只会收到一条警告。

因此,如果您在应用程序中仅使用对象列表,那么 List 和 List 之间实际上没有区别。但从 Java 1.5 开始,使用泛型为应用程序提供编译时类型安全是一个好主意。

Suppose you've got two lists:

List<Object> mainList = new ArrayList<Object>();

and one with a raw type:

List rawList = new ArrayList();

Now let's do something like this:

List<Integer> intList = new ArrayList<Integer>();
intList.add(42);
mainList.addAll(intList);
rawList.addAll(intList);

But there is somewhere one more list which contains String objects:

List<String> strList = new ArrayList<String>().

When we try to call

strList.addAll(mainList)

we get a Compilation Error :

The method addAll(Collection<? extends String>) in the type List<String> is not applicable for the arguments (List<Object>).

But if we try to call

strList.addAll(rawList)

we get just a warning.

So if you are using only lists of objects in your application it's really no difference between List and List. But since Java 1.5 it's a good idea to use generics for providing compile-time type safety for your application.

流绪微梦 2024-12-18 11:51:45

原始类型(例如,没有 List)的一个特殊危险是原始类型甚至在其自己的声明之外也禁用检查

参考:Java显示原始类型危险的谜题。 (从第13页开始) 简短版本:因为在语句中使用了原始类型,所以编译器没有评估其他地方的泛型类型,因此发生了奇怪的运行时异常。

简短的回答:这是一个使用原始类型的警告,这是有充分理由的。使用无界通配符 List 优先于原始类型 List

One particular danger of raw types (e.g. List without the <?>) is that raw types disable checking even outside of their own declarations

Ref: a Java Puzzler that shows the danger of raw types. (starts at page 13) Short version: because a raw type was used in a statement, a generic type elsewhere was not evaluated by the compiler, and thus bizarre runtime exceptions happened.

Short answer: it's a warning to use a raw type, for good reason. Use an unbounded wildcard List<?> in strong preference to a raw type List

夏雨凉 2024-12-18 11:51:45

区别在于:

List<String> a = new ArrayList<String>();

List<Object> b = a; // compile error

List c = a; // fine

List与任何其他参数的 List 不兼容。但是,原始 List 与所有参数的 List 兼容。

The difference is this:

List<String> a = new ArrayList<String>();

List<Object> b = a; // compile error

List c = a; // fine

List<Object> is not compatible with a List of any other parameter. However, a raw List is compatible with Lists of all parameters.

抹茶夏天i‖ 2024-12-18 11:51:45

差异非常微妙。两者中更明显的是 List它是任意对象的列表(因为每个类都将 Object 作为超类)。现在与 List 的区别在于 List 是无类型的,因此根本不执行类型检查,这最终会导致某些警告并可能导致奇怪的运行时行为。虽然 Java 知道 List是一个可能包含任何内容的列表,但它不知道 List 的情况。因为如果您考虑“旧式”Java,您所知道的 List 过去仅被称为 List。这就是这两者之间的区别...List被定义为故意包含各种对象,而 List 则不是't。

我希望这能澄清问题。如果没有,请告诉我们还有哪些不清楚的地方。 :)

The difference is quite subtle. The more obivous of the two is List<Object> which ist a list of arbitrary objects (as every class has Object as super class). The difference to List now is that List is untyped and therefore no type checks at all are performed, which ultimately leads to certain warnings and can lead to weird runtime behavior. While Java knows that List<Object> is a list that might contain anything, it doesn't know that about a List. Because if you consider "old style" Java, what you know would call List<String> used to be called only List. And that's the difference between those two...List<Object> is defined to contain a wide spectrum of objects on purpose, while List isn't.

I hope that sort of clarifies the matter. If not, just tell us what's still unclear. :)

浅唱ヾ落雨殇 2024-12-18 11:51:45

Java 中的泛型对于预先(在编译时)进行更好的错误检查非常重要,因此,在运行时检查和调试期间花费的时间更少。

因此,没有直接的性能提升,但您很快就能捕获错误。

底线:它有助于加快开发速度。

我强烈建议您阅读以下内容: Java 编程语言中的泛型 [pdf ]

Generics in Java is important for better error checking before hand (at compile time) due to which, less time is spent during runtime checking and debugging.

So, there is no direct performance gains, but you are quickly able to catch errors.

Bottom line: It helps in speed up development.

I strongly recommend you to go through this: Generics in the Java Programming Language [pdf]

奶茶白久 2024-12-18 11:51:45

对于原始类型有一些特殊的处理;没有动力将它们全部枚举,因为无论如何都不鼓励使用原始类型。

让我们讨论一个更狭窄的问题,这可能是最相关的:给定一个对象,我们主要关心我们可以在它上面调用什么方法;为此,如果其类型是 ListList,有什么区别吗?

List的方法是List的方法,其中E替换为Object;原始类型List的方法是List方法的擦除。所以我们

List                                List<Object>

boolean add(Object)                 boolean add(Object)
Object get(int)                     Object get(int)
int size()                          int size()
boolean addAll(Collection)          boolean addAll(Collection<? extends Object>)
...                                 ...

他们的方法大部分都是一样的!从这一点来看,这两种类型很难区分。

There are quiet some special treatments about raw types; there's no incentive to enumerate them all, since raw types are discouraged anyway.

Let's talk about a narrower problem, which is probably most pertinent: given an object, we mostly care about what methods we can invoke on it; for that purpose, is there any difference if its type is List or List<Object>?

The methods of List<Object> are methods of List<E> with E substituted with Object; The methods of raw type List is the erasure of methods of List<E>. So we have

List                                List<Object>

boolean add(Object)                 boolean add(Object)
Object get(int)                     Object get(int)
int size()                          int size()
boolean addAll(Collection)          boolean addAll(Collection<? extends Object>)
...                                 ...

most of their methods are the same! In that regard, the two types are hard to distinguish.

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