gpt4 book ai didi

Java 没有参数化集合

转载 作者:行者123 更新时间:2023-11-29 07:05:02 25 4
gpt4 key购买 nike

所以我注意到做类似的事情:

LinkedList list = new LinkedList();

编译器给你一个警告,所以我总是被教导正确的方法是:

LinkedList<Integer> list = new LinkedList<Integer>();

但我注意到这样做

LinkedList<Integer> list = new LinkedList<>();

也不发出警告,虽然我不太明白为什么。有人可以向我解释这最后两个例子之间的区别,如果一个比另一个更可取,无论是在风格上还是在实践上?

最佳答案

The Diamond

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box<Integer> with the following statement:

Box<Integer> integerBox = new Box<>();

For more information on diamond notation and type inference, see Type Inference.

阅读更多 herehere

关于Java 没有参数化集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20776544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com