GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Jagged Array in Java - GeeksforGeeks Jagged Array in JavaLast Updated : 28 Sep, 2026 A Jagged Array in Java is a multidimensional array in which each row can have a different number of elements. Unlike a regular 2D array, where rows usually have the same number of columns, a jagged array allows each row to have its own length.
For example:
Here, the rows contain 3, 2, and 4 elements respectively, making it a jagged array. ![]() Creating a Jagged ArrayFirst, create the outer array by specifying only the number of rows. Then, allocate a different size to each row.
Output 0 1 2 3 4 Explanation: In this example, arr[0] contains 3 elements, whereas arr[1] contains only 2 elements. Declaration and Initialization of Jagged ArrayA jagged array can be declared by specifying the number of rows while leaving the column size unspecified. Syntax
Different Ways to Initialize a Jagged ArrayMethod 1: Using new for Each RowIn this method, both the outer array and each inner array are explicitly created using the new keyword.
Method 2: Omitting new int[][]Here, Java automatically determines the outer array structure from the provided inner arrays, so new int[][] can be omitted.
Method 3: Using Array Initializer SyntaxThis is the simplest and most concise approach, where both the outer and inner arrays are initialized directly using values.
Note: All three approaches create a jagged array containing rows of different lengths.Example 1: Jagged Array with Increasing Row SizeIn this example, row i contains i + 1 elements.
Output Contents of 2D Jagged Array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Explanation: The first row contains one element, the second row contains two elements, and so on. Each row is allocated independently using new int[i + 1]. Example 2: Jagged Array Using User InputThe size and elements of each row can also be provided at runtime.
Output: The jagged array is: Explanation: The number of rows and the size of every row are entered at runtime. Since each row is allocated separately, different rows can contain different numbers of elements. Advantages of Jagged Arrays
Limitations of Jagged Arrays
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |