'length' array attribute

'length' array attribute will tell us the size of an array (i.e. the number of elements that an array can hold).

Lets implement this on Eclipse IDE:

1. Create 'ArrayLengthDemo' class under any project as shown below:



2. Declare two arrays as shown below:



3. Print the size of the declared array variables a[] and b[] as shown below:


4. Save and Run the 'ArrayLengthDemo' class 
5. Observe that the output is displayed in console as shown below:



Download this project:

Click here to download the project containing the class file used in this post (You can download the project and import into Eclipse IDE on your machine)

The above example is just for your understanding. Lets create some real time example where you can use this length attribute.

Lets implement the following program on Eclipse IDE 

Program: Assign and print the values of array variable with the help of 'length' attribute.

1. Create 'AssingAndPrint' class under any project as shown below:



2. Create an array a[] as shown below:



3. Assign values to all the elements in the array using for loop and length attribute as shown below:


4. Print the values of all the elements in the array a[] using for loop and length attribute as show below:

5.Save and Run the 'AssignAndPrint' class
6. Observe that the output is displayed in the console as shown below: