Rabu, 26 Maret 2014

How to create and initialize Anonymous array in Java Example




Anonymous arrays in Java is an Array without any name, just like Anonymous inner classes and policy of using Anonymous array is just create, initialize and use it, Since it doesn't have any name you can not reuse it. Anonymous array was a good way to implement variable argument methods before Java introduced varargs in Java5. You had the liberty to create array of any length and pass that to method which operate on anonymous array. Classical example of such variable argument method is aggregate function like sum(), avg(), min(), max() etc. In this java anonymous array tutorial we will how to create anonymous array, how to initialize them and example of anonymous array as variable argument method.




Anonymous array in Java Example Tutorial







How to create Anonymous array in Java



Anonymous array example java create initializeAnonymous array follows same syntax like normal array in Java e.g. new [] { }; , only difference is that after creating Anonymous array we don't store it on any reference variable. here is


few examples of creating anonymous array in java:






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');






as you have noticed just like anonymous class, creation and initialization of anonymous array is done on same time. you initialize them in same line where you create using new(). as they don't have name there is no way you can initialize() them later.





Anonymous Array Example in Java



Best use of anonymous array is to implement variable argument method which can be invoked with different number on arguments. these methods except an array type and when code invokes this method it creates an anonymous array of different length and pass to method for processing. here is complete code example of anonymous array method:






public class AnnonymousArrayExample {





    public static void main(String[] args) {


       


        //calling method with anonymous array argument


        System.out.println("first total of numbers: " + sum(new int[]{ 1, 2,3,4}));


        System.out.println("second total of numbers: " + sum(new int[]{ 1, 2,3,4,5,6,}));


      


    }


    


    //method which takes an array as argument


    public static int sum(int[] numbers){


        int total = 0;


        for(int i: numbers){


            total = total + i;


        }


        return total;


    }


}






same method sum() can also be implemented using varargs in Java like public static int sum(int... numbers) but you cannot have sum(int[] numbers) and sum(int... numbers) method in one class Java will throw compilation error because it internally used anonymous array to pass variable argument list.





Related Java Tutorial






























Source:http://javarevisited.blogspot.com/2012/01/anonymous-array-example-java-create.html

Tidak ada komentar:

Posting Komentar