Dec 7, 2021

Length of an Array in Java | Java Program to find Length of an Array

In this article you will learn how to find array length in java. But, before learning how to find array length, you should know exactly array length means what.

In short array length means the limit of elements that array can hold. See in the below diagram we have stored 7 elements in array so length of this array is 7.

  
Array length in java, array index in java, length of an array



As shown in above image array length means last index + 1. So in above image array last index is 6. so here array length is 7.


  Java Program to find Length of an Array:



public class ArrayLengthExample {

	public static void main(String[] args) {
		//defining array of length 10
		int arr[] = new int[10];
		
		System.out.println("Length of the array is : "+arr.length);
	}

}



  Output of the above Program is :

Length of the array is : 10



  Let's see one more Example to find Array Length:



public class ArrayLengthExample2 {

	public static void main(String[] args) {
		//initializing an array of type string
		String str[] = {"Apple","Mango","banana"};
		
		System.out.println("Size of the array is : "+str.length);
	}

}



  Output:

Size of the array is : 3




No comments:

Post a Comment

If you have any doubts, please discuss here...