Java 数组和 ArrayList 声明中的冗余措辞 [英] Redundant wording in a Java array and ArrayList declaration
问题描述
Java 中典型的 ArrayList
声明具有以下类型:
A typical ArrayList
declaration in Java is of the following type:
ArrayList<Object> name = new ArrayList<Object>();
类似地,对于一个数组,我们有:
Similarly, for an array, we have:
Object[] name = new Object[size];
现在,我们肯定不能有以下类型的声明:
Now, we certainly can not have a declaration of the following type:
ArrayList<Object> name = new ArrayList<AnotherObject>();
两次指定同一个对象的类型和名称在我看来一直是多余的.上面声明中的冗余可以通过这样的方式轻松消除:
The specifying of the type and the name of the same object two times has always seemed quite superfluous to me. The redundancy in the above declaration can be easily done away with something like this:
ArrayList<Object> name = new();
是否有任何具体的原因让我错过了为什么它是这样完成的?
Is there any specific reason that I am missing out as to why it is done the way it is done?
推荐答案
不是.至少,不再是了.现在你可以这样做:
It isn't. At least, not any more. Now you can do:
ArrayList<Object> name = new ArrayList<>();
您不能忽略第二个 ArrayList
,因为它不一定要匹配第一个.例如,您可以这样做:
You can't leave off the second ArrayList
because it doesn't necessarily have to match the first. For example, you can do this:
List<Object> name = new ArrayList<>();
然后你会得到一个 ArrayList
但把它看作一个 List
.
and you'll get an ArrayList
but see it as a List
.
这篇关于Java 数组和 ArrayList 声明中的冗余措辞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!