Prerequisite : Arrays in Java
Jagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D arrays but with variable number of columns in each row. These type of arrays are also known as Jagged arrays.
Following are Java programs to demonstrate the above concept.
// Program to demonstrate 2-D jagged array in Java class Main { public static void main(String[] args) { // Declaring 2-D array with 2 rows int arr[][] = new int[2][]; // Making the above array Jagged // First row has 3 columns arr[0] = new int[3]; // Second row has 2 columns arr[1] = new int[2]; // Initializing array int count = 0; for (int i=0; i<arr.length; i++) for(int j=0; j<arr[i].length; j++) arr[i][j] = count++; // Displaying the values of 2D Jagged array System.out.println("Contents of 2D Jagged Array"); for (int i=0; i<arr.length; i++) { for (int j=0; j<arr[i].length; j++) System.out.print(arr[i][j] + " "); System.out.println(); } } } |
Output:
Contents of 2D Jagged Array 0 1 2 3 4
Following is another example where i’th row has i columns, i.e., first row has 1 element, second row has two elements and so on.
// Another Java program to demonstrate 2-D jagged // array such that first row has 1 element, second // row has two elements and so on. class Main { public static void main(String[] args) { int r = 5; // Declaring 2-D array with 5 rows int arr[][] = new int[r][]; // Creating a 2D array such that first row // has 1 element, second row has two // elements and so on. for (int i=0; i<arr.length; i++) arr[i] = new int[i+1]; // Initializing array int count = 0; for (int i=0; i<arr.length; i++) for(int j=0; j<arr[i].length; j++) arr[i][j] = count++; // Displaying the values of 2D Jagged array System.out.println("Contents of 2D Jagged Array"); for (int i=0; i<arr.length; i++) { for (int j=0; j<arr[i].length; j++) System.out.print(arr[i][j] + " "); System.out.println(); } } } |
Output:
Contents of 2D Jagged Array 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
This article is contributed by Rahul Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.Lang.Float class in Java
- Java.io.BufferedInputStream class in Java
- Java.io.ObjectInputStream Class in Java | Set 1
- Java.util.BitSet class in Java with Examples | Set 1
- Java.io.File Class in Java
- Java.io.BufferedWriter class methods in Java
- Java.io.DataOutputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 1
- Java.io.InputStream Class in Java
- Java.io.SequenceInputStream in Java
- Java.io.StreamTokenizer Class in Java | Set 2
- Java.io.Console class in Java
- Java.io.FilterOutputStream Class in Java
- Java.io.StringWriter class in Java
- Java.io.CharArrayReader Class in Java
- Java.io.FileInputStream Class in Java
- Java.io.CharArrayWriter class in Java | Set 1
- Java.io.CharArrayWriter class in Java | Set 2
- Java.io.DataInputStream class in Java | Set 1

