If you mean anonymous arrays like anonymous class where it was declared and instantiated at the same time without a name. The example below shows when you would use it.
For example, you have a method which expects an array of int as the argument in the parameter list.
public void fillPolygon(int[] xPoints, int[] yPoints, nPoints)
To invoke this method:
int[] xPoints = {1,2,3};
int[] yPoints = {4,5,6};
g.fillPolygon(xPoints, yPoints, 3);
You can also write it as:
g.fillPolygon(new int[]{1,2,3}, new int[]{4,5,6}, 3);
In what scenario anonymous arrays are used?
A: Use it when you are only going to use it once because you do not keep a reference (name) for the arrays.
Q: So what is the benefit of using "anonymous array"?
A: It keeps your codes concise.
Answer from user3437460 on Stack OverflowIf you mean anonymous arrays like anonymous class where it was declared and instantiated at the same time without a name. The example below shows when you would use it.
For example, you have a method which expects an array of int as the argument in the parameter list.
public void fillPolygon(int[] xPoints, int[] yPoints, nPoints)
To invoke this method:
int[] xPoints = {1,2,3};
int[] yPoints = {4,5,6};
g.fillPolygon(xPoints, yPoints, 3);
You can also write it as:
g.fillPolygon(new int[]{1,2,3}, new int[]{4,5,6}, 3);
In what scenario anonymous arrays are used?
A: Use it when you are only going to use it once because you do not keep a reference (name) for the arrays.
Q: So what is the benefit of using "anonymous array"?
A: It keeps your codes concise.
Hello imagine that you have the following code:
public class Test {
public static void printArray(int []arr){
for (int i : arr) {
System.out.print(i);
}
}
public static void main(String[] args) {
int arr[]={1,2,3,4,5,6};
printArray(arr);
}
}
The output is 123456. If you want to call the printArrayMethod in a single line you have to use a anonymous array like the following:
printArray(new int[]{1,2,3,4,5,6});
You cant use something like the following:
printArray({1,2,3,4,5,6});
It is going to provoke a compilation fail.
And the question is, Why do I need to use an anonymous array and not a normal array like the first example.
Check the following code:
public class Test {
public static void printArray(int []arr){
for (int i : arr) {
System.out.print(i);
}
}
public static void main(String[] args) {
//Many code
printArray(new int[]{1,2,3,4,5,6});
//Many code
}
}
If you use a normal array this array is going to alive in your heap during all the main method execution and with the anonymous array you are only use this array to the printArray method and after this the object is going to be eligible to the garbage collector.
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 :)
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...
Remember that new Object() {...} gives you a value not a type.
So, here is how you could create an array of instances of anonymous classes:
final Object[] anonArray = new Object[]{
new Object(){
public String foo = "bar1";
},
new Object(){
public String foo = "bar2";
},
new Object(){
public String foo = "bar3";
},
new Object(){
public String foo = "bar4";
}
};
Or if you want an array with the same anonymous class instance 4 times:
final Object anonInstance = new Object(){
public String foo = "bar1";
};
final Object[] anonArray = new Object[]{
anonInstance, anonInstance, anonInstance, anonInstance
};
Or if you want 4 distinct instances of the same anonymous class:
final Object[] anonArray = new Object[4];
for (int i = 0; i < anonArray.length; i++) {
// Just to show we can do this ....
final String bar = "bar" + 1;
anonArray[i] = new Object(){
public String foo = bar; // Look!
};
}
Note that in all of the above the array type is Object[] not "array of the anonymous subclass of Object". It is difficult to conceive that this would matter, but if it did matter you would need to resort to reflection to create the array class. (See @ernest_k's answer.)
Or just declare an array of an appropriate custom class or interface, and create anon subclasses from that class / interface. Anonymous subclasses of Object are not exactly practical ...
The syntax new Object() {} creates an instance of the anonymous class. It does not offer a way to declare an array of the anonymous class.
This should really be discouraged, but if you need to have your anonymous class as the array's component type, you can use java.lang.reflect.Array:
Object o = new Object() {
public String foo = "bar";
};
Object[] anonArray = (Object[]) java.lang.reflect.Array.newInstance(o.getClass(), 4);
anonArray[0] = o;
That is probably going to have a hard time passing code review (you can't construct other elements without reflection). So, an alternative: declare a local class in your method:
class Anon {
public String foo = "bar";
};
Anon[] anonArray = new Anon[4];
The above Anon is local to the method (it's an underused feature of the language)