Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Anonymous Array

Anonymous Array

In java it is perfectly legal to create an anonymous array using the following syntax.

new <type>[] { <list of values>};

Anonymous array example

new int[]{1,2,3};

The above given example creates a nameless array and initializes it. Here, neither name of the array nor the size is specified. It also creates a array which can be assigned to reference or can be passed as a parameter to any method.

 
Anonymous array program

  1. public AnonymousArrayExample{
  2.  
  3.         public static void main(String args[]){
  4.  
  5.                 System.out.println(“Length of array is “ + findLength(new int[]{1,2,3}));
  6.  
  7.         }
  8.  
  9.         public static findLength(int[] array){
  10.  
  11.                 return array.lentgh;
  12.  
  13.         }
  14.  
  15. }

The above given program will print the length of the anonymous array which is being passed to the static method.

This brings us end of Java Array Tutorial. Please let us know your views by leaving a comment in comments section.


<< Multidimensional Array Example
Your rating: None Average: 2.7 (3 votes)
Share this
Anonymous's picture

public class Anonymous{

public class Anonymous{ public static void main(String args[]){ System.out.println("The array elements are: "); findLength(new int[]{1,2,3,4,5}); } public static void findLength(int[] arr){ for (int i=0;i
Anonymous's picture

thanks i dont know this till

thanks i dont know this till now
Anonymous's picture

good explanation

good explanation
Anonymous's picture

please wirte next time good

please wirte next time good code. valid code would be nice ;) public class AnonymousArrayExample{ public static void main(String args[]){ System.out.println("Length of array is" + findLength(new int[]{1,2,3})); } public static int findLength(int[] array){ return array.length; } }
Anonymous's picture

It should be "array.length;"

It should be "array.length;" and not "array.lentgh;"

Post new comment