Jagged Array in JavaLast Updated : 6 Jan 2026 In Java, a jagged array is a type of multidimensional array in which each row can have a different number of columns. In this chapter, we will learn what a jagged array is, how it is declared and initialized, and how to work with jagged arrays using simple examples. What is Jagged Array in Java?A jagged array is a collection of arrays where each array may contain a varying number of elements. A two-dimensional array, in contrast, requires all rows and columns to have the same length. Jagged arrays are also known as "ragged arrays" or "irregular arrays". They can be created by specifying the size of each array in the declaration. For example, a jagged array with three rows can have the first row with three elements, the second with two elements, and the third with four elements. Jagged Array DeclarationA jagged array is declared by first specifying the number of rows and then allocating each row separately with different column sizes. SyntaxHere is the syntax of declaring a jagged array: Here, the datatype is the data type of the elements in the array, numRows is the number of rows in the jagged array, and numColumns1, numColumns2, ..., numColumnsN are the number of columns in each row. Users should be aware that the number of columns in each row is flexible. The first line creates an array of arrays with numRows rows. Each row is initialized to null by default. The subsequent lines initialize each row of the jagged array. You create a new one-dimensional array of numColumns elements for each row and assign it to the corresponding row in the jagged array. Ways to Initialize a Jagged ArrayApart from the standard approach of declaring and initializing a jagged array, there are several different ways to initialize a jagged array in Java. 1. Using Array LiteralsWe can use array literals to initialize the jagged array elements like this directly: Here, we create a jagged array with three rows, where the first row has two elements, the second row has three elements, and the third row has four elements. ExampleCompile and RunOutput: 1 2 3 4 5 6 7 8 9 2. Using Nested LoopsWe can also use nested loops to initialize the jagged array elements when the elements are to be programmatically generated. Utilizing such an approach can be beneficial. Here, we design a jagged array with three rows, the first of which contains one element, the second two, and the third three. Using a simple formula, the nested loops generate the elements in the jagged array. ExampleCompile and RunOutput: 1 2 3 4 5 6 7 8 9 10 Jagged Array Using Java 8 StreamsWith Java 8 streams, you can create and initialize a jagged array in a single line using the Arrays.stream and toArray methods like this: Here, we make a three-row jagged array with two elements in the first row, three in the second row, and four in the third row. The Arrays.stream method converts each one-dimensional array into a stream of integers, and then the toArray method collects the stream into a jagged array. ExampleThe following example demonstrates how to create jagged array using Java 8 streams. Output: 1 2 3 4 5 6 7 8 9 Dynamic Jagged ArrayIn Java, we can create a dynamic jagged array by creating an array of arrays, where each sub-array represents a row in the two-dimensional array. After thar, we can allocate memory to each sub-array to specify its number of columns. ExampleThe following example shows the working of dynamic jagged array in Java. Output: Enter the number of sub-arrays: 3 Enter the size of sub-array 1: 2 Enter the element at index 0 of sub-array 1: 1 Enter the element at index 1 of sub-array 1: 2 Enter the size of sub-array 2: 3 Enter the element at index 0 of sub-array 2: 3 Enter the element at index 1 of sub-array 2: 4 Enter the element at index 2 of sub-array 2: 5 Enter the size of sub-array 3: 1 Enter the element at index 0 of sub-array 3: 6 The jagged array is: 1 2 3 4 5 6 Jagged Arrays Vs Multi-Dimensional ArraysA jagged array is not the same as a multi-dimensional array. There are some differences between Jagged array and multi-dimensional array that are described in the following table.
Next TopicArray Programs in Java |
We request you to subscribe our newsletter for upcoming updates.