In this lesson, you will learn
Note: Memory allocation for a multidimensional array requires specifying the first dimension, allowing for the separate allocation of remaining dimensions, as demonstrated in the code.
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
The below figure shows the conceptual view of a 2-D Jagged Array.

public class JaggedArrayExample {
public static void main(String[] args) {
// Declare a jagged array with 3 rows
int[][] jaggedArray = new int[3][];
// Initialize the inner arrays with different lengths
jaggedArray[0] = new int[] { 1, 2, 3 };
jaggedArray[1] = new int[] { 4, 5 };
jaggedArray[2] = new int[] { 6, 7, 8, 9 };
// Access and print the elements of the jagged array
System.out.println("Jagged Array Elements:");
for (int i = 0; i < jaggedArray.length; i++) {
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
}
System.out.println(); // Move to the next line after each row
}
//Another way to initialize a jagged array.
int[][] anotherJaggedArray = {
{10,11},
{12,13,14,15},
{16}
};
System.out.println("Another Jagged Array Elements:");
for (int i = 0; i < anotherJaggedArray.length; i++) {
for (int j = 0; j < anotherJaggedArray[i].length; j++) {
System.out.print(anotherJaggedArray[i][j] + " ");
}
System.out.println();
}
}
}
Jagged Array Elements:
1 2 3
4 5
6 7 8 9
Another Jagged Array Elements:
10 11
12 13 14 15
16
public class WeeklyHours {
public static void main(String[] args) {
// Creating a jagged array for hours
// worked on different days of the week
double[][] weekHours = {
{8.5}, // Monday
{8.0, 3.5}, // Tuesday (two shifts)
{8.0}, // Wednesday
{8.0, 4.0}, // Thursday (two shifts)
{6.5}, // Friday
{}, // Saturday (no work)
{} // Sunday (no work)
};
// Displaying hours
String[] days = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};
for (int i = 0; i < weekHours.length; i++) {
System.out.print(days[i] + ": ");
for (double hours : weekHours[i]) {
System.out.print(hours + " ");
}
System.out.println();
}
}
}
Monday: 8.5
Tuesday: 8.0 3.5
Wednesday: 8.0
Thursday: 8.0 4.0
Friday: 6.5
Saturday:
Sunday:
This example demonstrates a jagged array representing grades of students in different subjects, where each subject might have a different number of assessments.
public class StudentGrades {
public static void main(String[] args) {
// Grades for three subjects, each having
// a different number of assessments
int[][] grades = {
{87, 92}, // Grades in Subject 1 (e.g., Math)
{78, 85, 90}, // Grades in Subject 2 (e.g., Science)
{85, 80} // Grades in Subject 3 (e.g., History)
};
// Subject names
String[] subjects = {"Math", "Science", "History"};
// Displaying grades for each subject
for (int i = 0; i < grades.length; i++) {
System.out.print(subjects[i] + " Grades: ");
for (int grade : grades[i]) {
System.out.print(grade + " ");
}
System.out.println();
}
}
}
Math Grades: 87 92
Science Grades: 78 85 90
History Grades: 85 80
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.