What is a Jagged Array in Java
Jagged array in Java are Multi-Dimensional arrays with different array sizes. Jagged Arrays are sometimes termed as Ragged Arrays. Lets go through a quick tutorial
Jagged Array in Java
package com.javaindetail.multidimensional;
public class JaggedArray {
public static void main(String[] args) {
// Jagged Two Dimensional Array as it had differnet length arrays
int[][] jaggedArray = new int[3][];
// One Dimensional Array of different lengths
int[] one = { 1, 2, 3 };
int[] two = { 4, 5, 6, 7 };
int[] three = { 8, 9, 10, 11, 12 };
// Initializing elements of Jagged Array
jaggedArray[0] = one;
jaggedArray[1] = two;
jaggedArray[2] = three;
// Printing Jagged Array
System.out.println("Jagged Array");
for (int i = 0; i < jaggedArray.length; i++) {
System.out.print("[");
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.print("]");
System.out.println();
}
}
}
Output
Jagged Array
[1 2 3 ]
[4 5 6 7 ]
[8 9 10 11 12 ]
Enjoy Reading This Article?
Here are some more articles you might like to read next: