In this tutorial you will learn about the Java Program to Find Length of Array and its application with practical example.
Java Program to Find Length of Array
In this tutorial, we will learn to create a Java program that will Find the Length of an Array in Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- For loop in Java programming.
- Conditional Statements in Java programming.
- Array in Java programming.
What is An array?
The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single and multidimensional.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Take the array elements from the user. 3. Using the size of function to finding the array size. 4. Printing the size of the array. 5. End the program. |
Find Length or Size of an Array:-
As we all know, the array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First, will take the number of elements of an array from the user. Then will Find the Length or Size of an Array. And at last, we will print the Length or Size of an Array.
With the help of this program, we can print the Length or Size of an Array.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class ArrayLength { public static void main(String args[]){ //Declaring the variable and creating String array String[] arry = new String[]{"Java", "tutorial", "at", "w3adda"}; /* * To get length of array, use length property of array. */ int length = arry.length; //Printing the length of the array System.out.println("String array length is: " + length); //print elements of an array from the program. for(int i=0; i < length; i++){ System.out.println(arry[i]); } } } |
Output:-
In the above program, we have first initialized the required variable.
- i = it will hold the integer value.
- arr[] = it will hold the integer value.
Finding the length of the array.
Printing the length of the array.
Printing the elements of the array.