Saved Bookmarks
| 1. |
What is a Jagged Array in Java? |
|
Answer» Jagged arrays are multidimensional arrays in which the member arrays are of different sizes. As an example, we can make a 2D ARRAY where the first array contains three elements, and the SECOND array consists of four elements. Below is an example demonstrating the concept of jagged arrays. public class InterviewBit { public static void MAIN(String[] args){ int[][] 2dArray = new int[2][]; 2dArray[0] = new int[3]; 2dArray[1] = new int[4]; int counter = 0; for(int row=0; row <2dArray.length; row++){ for(int COL=0; col < 2dArray[row].length; col++){ 2dArray[row][col] = counter++; } } for(int row=0; row < 2dArray.length; row++){ System.out.println(); for(int col=0; col < 2dArray[row].length; col++){ System.out.print(2dnArray[row][col] + " "); } } }}Output: 0 1 23 4 5 6 |
|