Anonymous Arrays

Anonymous Arrays·

 Sometimes we can create an array without name such type of nameless arrays are
called anonymous arrays.
· The main objective of anonymous arrays is “just for instant use”.
· We can create anonymous array as follows.
new int[]{10,20,30,40};(valid)
new int[][]{{10,20},{30,40}};(valid)
· At the time of anonymous array creation we can’t specify the size otherwise we will get
compile time error.
Example:
new int[3]{10,20,30,40};//C.E:';' expected(invalid)
new int[]{10,20,30,40};(valid)
· Based on our programming requirement we can give the name for anonymous array
then it is no longer anonymous.

Example:
class Test
{
public static void main(String[] args)
{
System.out.println(sum(new int[]{10,20,30,40}));//100
}
public static int sum(int[] x)
{
int total=0;
for(int x1:x)
{
total=total+x1;
}
return total;
}

}


SHARE

About df

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment