# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
How to Find Size or Length of Array in Java | Array Programs in Java – 1 | In this post, we will discuss how to find the size or length of an array in Java? How to find the size or length of the multidimensional array in Java?
The length is an in-built property of the array variable, which stores the information about the number of elements in the array.
Whenever we initialize an array then by default length property is assigned to the array and we can access them through arrayVariable.length
Java program to display size or length of an array in Java
public class ArrayLength {
public static void main(String[] args) {
// declare and initialize an array
int arr[] = {10, 20, 30, 40, 50};
// display array length
System.out.print("The length of the given array = ");
System.out.println(arr.length);
}
}
Output:-
The length of the given array = 5
Using the length property we can iterate through the array,
class TestArray{
public static void main(String[] args) {
// defining array
int[] a = {10,20,30,40,50};
// display size of array
System.out.println("Size = "+ a.length);
// display array using length property
System.out.println("Array elements:");
for(int i=0; i < a.length; i++){
System.out.print(a[i]+"\t");
}
}
}
Output:-
Size = 5
Array elements:
10 20 30 40 50
How to Find the length of Multidimensional Array in Java
Java Multidimensional arrays have multiple length properties, each for every dimension basis. For example, if we have a two-dimensional array arr then we can find parent length using arr.length, and child length using arr[index].length
public class ArrayLength {
public static void main(String[] args) {
// declare and initialize an array
int arr[][] = {{50,60},{70,80},{90,100}};
// display array length
System.out.print("The length of the given array = ");
System.out.println(arr.length);
System.out.println("arr[0] length = " + arr[0].length);
System.out.println("arr[1] length = " + arr[0].length);
System.out.println("arr[2] length = " + arr[0].length);
}
}
Output:-
The length of the given array = 3
arr[0] length = 2
arr[1] length = 2
arr[2] length = 2
It can be also displayed as:-
// display array length
System.out.print("The length of the given array = ");
System.out.println(arr.length);
for(int i=0; i<arr.length; i++) {
System.out.println("arr["+ i +"] length = " + arr[0].length);
}
The above length property can be used to manipulate the multi-dimensional array-like to display the multi-dimensional array.
// display 2D array using for loop
for(int i=0; i<arr.length; i++) {
for(int j=0; j<arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
}
Java program to demonstrate these above points,
class TestArray{
public static void main(String[] args) {
// 2d array
int[][] a = {{10,20},{30,40},{50,60}};
// display size of array
System.out.println("2D array size = "+ a.length);
System.out.println("First row size = "+ a[0].length);
System.out.println("Second row size = "+ a[1].length);
// display array using length property
System.out.println("Array elements:");
for(int i=0; i < a.length; i++){
for (int j=0; j < a[i].length; j++) {
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
}
Output:-
2D array size = 3
First row size = 2
Second row size = 2
Array elements:
10 20
30 40
50 60
Size of Jagged Array in Java
Jagged Array in Java:- A multi-dimensional array with different sizes child array is called a Jagged array. It creates a table with different sizes of columns in a row.
class TestArray{
public static void main(String[] args) {
int[][] a = {{10,20},{30},{50,60,70},{80,90}};
// display size of array
System.out.println("Array size = "+ a.length);
System.out.println("First row size = "+ a[0].length);
System.out.println("Second row size = "+ a[1].length);
// display array using length property
System.out.println("Array elements:");
for(int i=0; i < a.length; i++){
for (int j=0; j < a[i].length; j++) {
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}
Output:-
Array size = 4
First row size = 2
Second row size = 1
Array elements:
10 20
30
50 60 70
80 90
Size of Three Dimensional Java Array
Three dimensional (3D) array contains three properties:- arr.length, arr[index].length, and arr[index-1][index-2].length. Below program demonstrate it,
public class ArrayLength {
public static void main(String[] args) {
// declare and initialize an array
int[][][] arr = { {{1,2},{3,4},{5,6}}, {{7,8},{9,1},{2,3}} };
// display array length
System.out.print("The length of the given array = ");
System.out.println(arr.length);
System.out.println("arr[0] length = " + arr[0].length);
System.out.println("arr[0][0] length = " + arr[0][0].length);
System.out.println("arr[0][1] length = " + arr[0][1].length);
System.out.println("arr[0][2] length = " + arr[0][2].length);
}
}
Output:-
The length of the given array = 2
arr[0] length = 3
arr[0][0] length = 2
arr[0][1] length = 2
arr[0][2] length = 2
We can use these length properties to display the three-dimensional array,
// displaying three dimension array in Java
// using for loop and length property
for(int i=0; i < arr.length; i++){
for(int j=0; j < arr[i].length; j++){
for(int k=0; k < arr[i][j].length; k++){
System.out.print( arr[i][j][k] + "\t");
}
}
}
Output:-
1 2 3 4 5 6 7 8 9 1 2 3
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Thanks for clearing my doubt.
Amazing content of Array in Java. All doubts are cleared for me in this post. Thank you so much!
Extremely enjoyed the basics of the array, in fact, all doubts are cleared now by practicing from here, Thaks a bunch!