What is Jagged Array ?
Before learning about Jagged Array, we need to know about array and its types, so here is: A short overview about array.
Array : Array is a special type of the variable in that we can store zero or more values of the similar data type.
Types of Array: In java Array is categorized in three category-
1. Single Dimensional Array
2. Multi Dimensional Array
3. Jagged Array
Single Dimensional Array :
A single dimensional array in Java is an array that holds a sequence of elements, all of the same type, accessed through an index
int[] arr=new int[5];
2. Multidimensional Array :
- A multidimensional array is an array of arrays.
- Multidimensional arrays are useful when you want to store data as a tabular form, like a table with rows and columns
int [][] arr= new int[3][3];
Note: Array always occupy the memory in contiguous manner
Ex:- int arr[][]=new int[3][5];
int[][] arr = {{1,2},{4,5},{7,8}};
EX:
int[][] intArray = new int[4][4]; //Declaration & Instantiation
//Below is the initialization
intArray[0][0] = 0;
intArray[0][1] = 1;
intArray[0][2] = 2;
intArray[0][3] = 3;
intArray[1][0] = 4;
intArray[1][1] = 5;
intArray[1][2] = 6;
intArray[1][3] = 7;
intArray[2][0] = 8;
intArray[2][1] = 9;
intArray[2][2] = 10;
intArray[2][3] = 11;
intArray[3][0] = 12;
intArray[3][1] = 13;
intArray[3][2] = 14;
intArray[3][3] = 15;
3. Jagged Array:
Jagged Array is also a kind of the Multi-Dimensional Array.
When we have the different-different number of the columns in the different-different rows. That process is known as Jagged Array.
Note : Array rows with different no of columns is known as jagged array
Declaration :
int[][] a = { {1,2,3}, {4,5,6,7}, {8,9}, {10,11,12,13,14} };