Redundant wording in Java array and ArrayList declarations
•
Java
A typical ArrayList declaration in Java has the following types:
ArrayList<Object> name = new ArrayList<Object>();
Similarly, for arrays, we have:
Object[] name = new Object[size];
Now, of course, we cannot declare the following types:
ArrayList<Object> name = new ArrayList<AnotherObject>();
Specifying the type and name of the same object twice always seems redundant to me Redundancy in the above statement can be easily accomplished by:
ArrayList<Object> name = new();
Is there any specific reason why I missed it? Why did it finish in its way?
Solution
This is not the case At least not anymore Now you can do this:
ArrayList<Object> name = new ArrayList<>();
You can't give up the second ArrayList because it doesn't have to match the first For example, you can do this:
List<Object> name = new ArrayList<>();
You'll get an ArrayList, but think of it as a list
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码