Java数组和ArrayList声明中的冗余措辞 [英] Redundant wording in a Java array and ArrayList declaration

查看:123
本文介绍了Java数组和ArrayList声明中的冗余措辞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

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