posted 15 years ago
Hi
An array can be declared, initialized in the following ways:
1. int[] arr1 = new int[4]; // elements contains default values '0'
2. int[] arr1 = {1,2,3,4} // anonymous array initialization
3. int[] arr1 = new int[]{1,2,3,4}
if you have a method that takes an int array as an argument as follows:
public void doSomething(int[] arr1){
//your code goes here...
}
while invoking the above method, you can invoke as follows:
doSomething({1,2,3,4});
1. if you dont need to refer to this array instance anywhere else in the method or class where the method doSomething() has been called then its useful to create an anonomyous array and send to the method doSomething() instead of creating an array variable in class or in the method before passing it to the method doSomething().
Hope it is clear.
Best Regards
Kris