问题
(I am studying for the Java Associate Exam OCJP 7)
A question asked to select examples of illegal initializations.
One of the answers was:-
int [] k= new int[2]{5,10};
The explanation said that when creating an anonymous array it was illegal to specify the size of the array.
From what I know this is NOT an anonymous array as it is named "k".
Calling a method such as :-
operateOnArray(new int[]{5,10});
Would have been an example of an anonymous array since it is not declared.
I can see that the "2" makes it illegal - but that does not make it anonymous,
Please can someone advise me?
回答1:
Mmmm... from what I've been taught (and also from what I've read), the definition of an Anonymous Array is: "Array without any name, it is an array just for create and use it". And since it does not have any name, you should not be able to reuse that array.
The best references I had when I prepared for the OCJP was:
anonymous int array : new int[] { 1, 2, 3, 4};
anonymous String array : new String[] {"one", "two", "three"};
anonymous char array : new char[] {'a', 'b', 'c');
You can notice that (and also you already know) these type of arrays have the creation and initialization at the same time (as you initialize them in the same line you create them using the new()
keyword without assigning to any variable and you would not be able to reuse it later).
So, from what you have mentioned, when you assign an array to a variable, even when you create it and initialize at the same line, it is being assigned to a variable so it is able to be reused later, it is not anonymous, it can be referenced, so I wonder why in the examples of "anonymous arrays" you see something like:
int [] k= new int[2]{5,10};
Anonymous means "not known by name" which is not the case in the line you specified since the array is assigned to a variable called "k". However, this one shows the property of anonymous array-object creation which is pointed by a reference variable "k", so if we write just "new int[]{1,2,3};" then this is how anonymous array-object can be created...
When I prepared for OCJP 7 I read many articles from this page, I would recommend it to you to go deeply in Java in the future :)
Reference: http://javarevisited.blogspot.com/2012/01/anonymous-array-example-java-create.html
You can find also good examples and articles there :)
回答2:
I think it will be like an anonymous method...
The anonymous array would be like myListView.setAdapter(this, new String [] {"Peter", "Paul", "Marry"});
Where the (new String [] {"Peter", "Paul", "Marry"}) is the anonymous array :-)
So a normal (not anonymous) array would be decleared (maybe in onCreate) like
String [] array = new String []...
While the anonymous one won't be decleared before usage...
回答3:
It is not so clear what exactly is the question, but I get the point clearly that there is a confusion with regards how to correctly use the concept of an anonymous array in Java. You can only declare an anonymous array as an argument to a function. All else attempts to declare anonymous array not in this context, failed. This quick video shows it: anonymous array as argument to function
回答4:
If you see this part of the code:
new int[2]{5,10}
in the first declaration and then
new int[]{5,10}
from the second declaration. It's basically the same
So, a declaration like:
new int[]{5,10}
is an anonymous array, no matters what happen next. If you do not set it to a variable, it remains anonymous.
来源:https://stackoverflow.com/questions/27274356/is-it-an-anonymous-array-if-the-resulting-array-is-assigned-to-a-variable