A jagged array is an array of arrays such that member arrays can be of different row sizes and column sizes. Jagged subarrays may also be null. For instance, the following code declares and populates a two dimensional int array whose first subarray is of four length, second subarray is of three length, and the last subarray is a fours length array:
Example :
int [ ] [ ] a = { { 10,20,30,40 } , { 10,20,30 } , { 10,20,30,50 } } ;
This program demonstrates the use of jagged arrays in Java. A jagged array is a two-dimensional array where each row may have a different number of columns. In this example, the array a has three rows with different lengths.
The program prints the elements of the jagged array using nested for loops. The outer loop iterates through each row of the array (i), while the inner loop iterates through the columns of each row (j).
The length of each row can be accessed using the .length property of the array. In this case, a.length gives the number of rows, and a[i].length gives the number of columns in row i. The program uses this information to print the elements of the array using the System.out.print() method, with a space separator between each element.
After printing all the elements of a row, the program calls System.out.println() to print a newline character, so that each row is printed on a separate line. Overall, this program demonstrates how to declare and use jagged arrays in Java, and how to print their elements using nested for loops.
public class jagged_Array { public static void main(String args[]) { //Jagged Array using For Loop in Java Programming int a[][] = { {10, 20, 30, 40},//4 {10, 20, 30},//3 {10, 20, 30, 50}//4 }; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { System.out.print(" "+a[i][j]); } System.out.println(""); } } }
10 20 30 40 10 20 30 10 20 30 50To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions