Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Jagged_Array_Java_Program

A jagged array in Java is an array of arrays where each sub-array can have a different number of elements, unlike a traditional 2D array. The document provides a Java program that initializes a jagged array, displays its elements, and calculates the sum of each row. The output shows the elements of each row along with their respective sums.

Uploaded by

Bijay Kc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Jagged_Array_Java_Program

A jagged array in Java is an array of arrays where each sub-array can have a different number of elements, unlike a traditional 2D array. The document provides a Java program that initializes a jagged array, displays its elements, and calculates the sum of each row. The output shows the elements of each row along with their respective sums.

Uploaded by

Bijay Kc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Jagged Array in Java

What is a Jagged Array?

A jagged array is an array of arrays where each sub-array can have a different number of elements.

Unlike a 2D array where each row has the same number of columns, jagged arrays allow rows to

have different lengths.

Java Program: Initialize and Display Jagged Array with Sum of Each Row

public class JaggedArrayExample {


public static void main(String[] args) {
// Initializing a jagged array
int[][] jaggedArray = {
{1, 2, 3},
{4, 5},
{6, 7, 8, 9}
};

// Displaying elements and calculating sum of each row


for (int i = 0; i < jaggedArray.length; i++) {
int sum = 0;
System.out.print("Row " + (i + 1) + ": ");
for (int j = 0; j < jaggedArray[i].length; j++) {
System.out.print(jaggedArray[i][j] + " ");
sum += jaggedArray[i][j];
}
System.out.println("=> Sum: " + sum);
}
}
}

Output:

Row 1: 1 2 3 => Sum: 6

Row 2: 4 5 => Sum: 9

Row 3: 6 7 8 9 => Sum: 30

You might also like