Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Jagged Array in Java

Jagged Array in Java | Basics of Java - Software Development PDF Download

Introduction

Jagged arrays, also known as ragged arrays, are a type of multidimensional array in Java that have different lengths for each row. Unlike regular rectangular arrays, where each row has the same number of columns, jagged arrays offer flexibility in handling uneven data structures. In this article, we'll explore the concept of jagged arrays, understand their structure, and learn how to work with them effectively in Java.

Understanding Jagged Arrays

A jagged array is an array of arrays, where each element of the outer array holds a reference to another array. The sizes of the inner arrays can vary, allowing us to create uneven or irregular structures. This flexibility is useful when dealing with data sets that have varying lengths or when we don't know the size of the data in advance.
Consider the following example to visualize a jagged array:

int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[2];

jaggedArray[1] = new int[4];

jaggedArray[2] = new int[3];

In the above code snippet, we create a jagged array with three rows. The first row has two elements, the second row has four elements, and the third row has three elements. This structure demonstrates how each row in a jagged array can have a different length.

Declaring and Initializing Jagged Arrays

To declare a jagged array, we need to specify the number of rows in the outer array. However, we do not need to specify the size of the inner arrays at the time of declaration. We can assign the sizes later during initialization.

Here's an example of declaring and initializing a jagged array:

int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[]{1, 2, 3};

jaggedArray[1] = new int[]{4, 5, 6, 7};

jaggedArray[2] = new int[]{8, 9};

In the code above, we declare a jagged array with three rows but leave the sizes of the inner arrays unspecified. Then, we initialize each row individually with different lengths using the curly braces {}.

Accessing Elements in a Jagged Array

To access elements in a jagged array, we use the row and column indices, similar to a regular two-dimensional array. However, since each row can have a different length, we need to consider the size of each inner array dynamically.
Consider the following example:

int[][] jaggedArray = {

    {1, 2, 3},

    {4, 5, 6, 7},

    {8, 9}

};


System.out.println(jaggedArray[0][1]);  // Output: 2

System.out.println(jaggedArray[1][2]);  // Output: 6

System.out.println(jaggedArray[2][0]);  // Output: 8

In this code snippet, we initialize a jagged array directly without explicitly declaring its size. Then, we access specific elements using row and column indices, just like in a regular two-dimensional array.

Manipulating Jagged Arrays

Jagged arrays can be modified by changing the size of the inner arrays or by adding/removing elements.

Changing the Size of Inner Arrays

We can change the size of an inner array in a jagged array by assigning a new array of a different length to a specific row.

Consider the following example:

int[][] jaggedArray = {

    {1, 2, 3},

    {4, 5, 6, 7},

    {8, 9}

};


jaggedArray[1] = new int[3];  // Change the size of the second row

In this code snippet, we change the size of the second row of the jagged array by assigning a new array of length 3. This operation allows us to modify the structure of the jagged array dynamically.

Adding Elements to a Jagged Array

To add elements to a jagged array, we create a new inner array with the desired length and assign it to a specific row.
Consider the following example:

int[][] jaggedArray = {

    {1, 2},

    {3, 4, 5}

};


int[] newRow = {6, 7, 8};

jaggedArray[2] = newRow;  // Add a new row to the jagged array

In this code snippet, we add a new row to the jagged array by creating a new inner array with elements 6, 7, and 8, and assigning it to the third row.

Removing Elements from a Jagged Array

To remove elements from a jagged array, we can assign a smaller inner array to a specific row, effectively discarding the unwanted elements.
Consider the following example:

int[][] jaggedArray = {

    {1, 2, 3},

    {4, 5, 6, 7},

    {8, 9}

};


jaggedArray[1] = new int[2];  // Remove elements from the second row

In this code snippet, we remove elements from the second row of the jagged array by assigning a new array of length 2. The discarded elements are effectively removed from the structure.

Sample Problems and Solutions

Here are some sample problems related to jagged arrays along with their solutions:

Problem 1: Calculate the sum of all elements in a jagged array.

int[][] jaggedArray = {

    {1, 2, 3},

    {4, 5},

    {6, 7, 8, 9}

};


int sum = 0;

for (int[] row : jaggedArray) {

    for (int element : row) {

        sum += element;

    }

}


System.out.println("Sum: " + sum);  // Output: 45

Problem 2: Find the row with the maximum sum of elements in a jagged array.

int[][] jaggedArray = {

    {1, 2, 3},

    {4, 5},

    {6, 7, 8, 9}

};


int maxSumRow = 0;

int maxSum = 0;


for (int i = 0; i < jaggedArray.length; i++) {

    int sum = 0;

    for (int element : jaggedArray[i]) {

        sum += element;

    }

    if (sum > maxSum) {

        maxSum = sum;

        maxSumRow = i;

    }

}


System.out.println("Row with maximum sum: " + maxSumRow);  // Output: 2

Conclusion

Jagged arrays offer flexibility in handling data structures with varying lengths. By allowing different sizes for each row, jagged arrays enable us to work with irregular data effectively. In this article, we learned about declaring, initializing, accessing, and manipulating jagged arrays in Java. With the provided examples and sample problems, you should now have a solid understanding of how to work with jagged arrays and apply them to solve different programming challenges.

The document Jagged Array in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests
60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Download the FREE EduRev App
Track your progress, build streaks, highlight & save important lessons and more!
Related Searches

Summary

,

Free

,

Viva Questions

,

Jagged Array in Java | Basics of Java - Software Development

,

MCQs

,

Important questions

,

Extra Questions

,

pdf

,

shortcuts and tricks

,

mock tests for examination

,

past year papers

,

Sample Paper

,

practice quizzes

,

study material

,

Previous Year Questions with Solutions

,

ppt

,

Jagged Array in Java | Basics of Java - Software Development

,

Exam

,

Semester Notes

,

video lectures

,

Jagged Array in Java | Basics of Java - Software Development

,

Objective type Questions

;