0% found this document useful (0 votes)
63 views24 pages

Jagged Array

This document provides examples and explanations of one-dimensional arrays, multi-dimensional arrays, and jagged arrays in Java. It demonstrates how to declare and initialize each type of array, access elements, and print the output. Key differences are highlighted, such as multi-dimensional arrays having the same number of columns in each row while jagged arrays can have a different number of columns per row. Sample outputs are included for code snippets showing how to iterate through each array type.

Uploaded by

Ram Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views24 pages

Jagged Array

This document provides examples and explanations of one-dimensional arrays, multi-dimensional arrays, and jagged arrays in Java. It demonstrates how to declare and initialize each type of array, access elements, and print the output. Key differences are highlighted, such as multi-dimensional arrays having the same number of columns in each row while jagged arrays can have a different number of columns per row. Sample outputs are included for code snippets showing how to iterate through each array type.

Uploaded by

Ram Kishore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

One Dimensional Array

- 5 3 3
arr[0] arr[1] arr[2]
arr
1 // Predict the output
2 import java.util.*;
3 class Main
4 {
5 public static void main(String args[])
6 {
7 int len;
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Array length : ");
10 len=sc.nextInt();
11 int a[]=new int[len];
12 for(int i=0; i<len; i++)
13 {
14 a[i] = sc.nextInt();
15 }
16 System.out.print("Elements in Array are :\n");
17 for(int i=0; i<len; i++)
18 {
19 System.out.print(a[i] + " ");
20 }
21 }
22 }
Multi Dimensional Array
0 1

5 6
- - - -
arr[0] arr[1] arr[2]
arr 9 5
1 // Predict the output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Scanner al = new Scanner(System.in);
8 int row = al.nextInt();
9 int column = al.nextInt();
10 int[][] arr = new int[row][column];
11 for (int i = 0; i < arr.length; i++)
12 {
13 for (int j = 0; j < arr[i].length; j++)
14 {
15 arr[i][j] = j;
16 System.out.print(arr[i][j] + " ");
17 }
18 System.out.println("");
19 }
20 }
21 }
22
Is it possible to create a Two dimensional array
with different columns in each row?
Jagged Array
0 1 2
arr[0] -
-
arr[1] - 3 4
arr
Real time Example for Jagged Array
1 public class Main{
2 public static void main(String[] args)
3 {
4 int[][] array = {{0,1,2,3,4},
5 {0,1,2},
6 {0,1,2,3},
7 {0,1},
8 {0,1,2,3,4,5},
9 {0},
10 {0,1,2,3},
11 {0,1,2,3}
12 };
13 for (int i = 0; i<array.length; i++)
14 {
15 for (int j = 0; j<array[i].length; j++)
16 {
17 System.out.print(array[i][j] + " ");
18 }
19 System.out.println();
20 }
21 }
22 }
OUTPUT

0 1 2 3 4
0 1 2
0 1 2 3
0 1
0 1 2 3 4 5
0
0 1 2 3
0 1 2 3
1 public class Main{
2 public static void main(String[] args)
3 {
4 int[][] arr = new int[3][];
5 arr[0] = new int[1];
6 arr[1] = new int[2];
7 arr[2] = new int[3];
8 for (int i = 0; i<arr.length; i++)
9 {
10 for (int j = 0; j<arr[i].length; j++)
11 {
12 System.out.print(arr[i][j] + " ");
13 }
14 System.out.println();
15 }
16 }
17 }
18
19
20
21
22
OUTPUT

0
0 0
0 0 0
1 public class Main{
2 public static void main(String[] args)
3 {
4 int[][] arr = new int[][]
5 {
6 new int[] { 1, 2, 3 },
7 new int[] { 4, 5, 6, 7 },
8 new int[] { 8, 9 },
9 };
10 for (int i = 0; i<arr.length; i++)
11 {
12 for (int j = 0; j<arr[i].length; j++)
13 {
14 System.out.print(arr[i][j] + " ");
15 }
16 System.out.println();
17 }
18 }
19 }
20
21
22
OUTPUT

1 2 3
4 5 6 7
8 9
1 //Predict the Output
2 import java.util.*;
3 public class Main {
4 public static void main(String[] args)
5 {
6 Scanner al = new Scanner(System.in);
7 int row = al.nextInt();
8 int arr[][] = new int[row][];
9 arr[0] = new int[3];
10 arr[1] = new int[2];
11 arr[2] = new int[5];
12 for (int i=0; i<arr.length; i++)
13 {
14 for(int j = 0; j < arr[i].length; j++)
15 {
16 arr[i][j] = j;
17 }
18 }
19
20
21
22
1 for (int i=0; i<arr.length; i++)
2 {
3 for (int j=0; j < arr[i].length; j++)
4 {
5 System.out.print(arr[i][j] + " ");
6 }
7 System.out.println();
8 }
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 // Difference between 1D Vs MULTI D Vs RAGGED ARRAY
2
3 import java.util.*;
4 public class Main
5 {
6 public static void main(String args[])
7 {
8 Scanner sc = new Scanner(System.in);
9 int row = sc.nextInt();
10 int column = sc.nextInt();
11 int a[] = new int[row];
12 int b[][] = new int[row][column];
13 int c[][] = new int[row][];
14 int i, j, k = 1, m = 1, n = 1;
15 System.out.print("One Dimensional Array :\n");
16 for(i = 0; i < row; i++)
17 {
18 a[i]= k++;
19 System.out.print(a[i] + " ");
20 }
21 System.out.println();
22
1 System.out.println("Multi Dimensional Array :");
2 for(i = 0; i < row; i++)
3 {
4 for(j = 0; j < column; j++)
5 {
6 b[i][j] = n++;
7 System.out.print(b[i][j] + " ");
8 }
9 System.out.println();
10 }
11
12
13
14
15
16
17
18
19
20
21
22
1 System.out.print("Ragged Array : \n");
2 for(i = 0; i < row; i++)
3 {
4 c[i]= new int[i+1];
5 }
6 for(i = 0; i < row; i++)
7 {
8 for(j = 0; j <= i; j++)
9 {
10 c[i][j] = m++;
11 System.out.print(c[i][j] + " ");
12 }
13 System.out.println();
14 }
15 }
16 }
17
18
19
20
21
22
1 // Predict the output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Scanner al = new Scanner(System.in);
8 int row = al.nextInt();
9 int arr[][] = new int[row][];
10 for(int i = 0;i<row;i++)
11 {
12 arr[i] = new int[al.nextInt()];
13 }
14 System.out.println("Elements are :");
15
16
17
18
19
20
21
22
1 for (int i=0; i<arr.length; i++)
2 {
3 for(int j=0; j<arr[i].length; j++)
4 {
5 arr[i][j] = al.nextInt();
6 }
7 }
8 for (int i=0; i<arr.length; i++)
9 {
10 for (int j=0; j<arr[i].length; j++)
11 {
12 System.out.print(arr[i][j] + " ");
13 }
14 System.out.println();
15 }
16 }
17 }
18
19
20
21
22
1 // Predict the output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 int[][][] test =
8 {
9 {
10 {1, -2, 3},
11 {2, 3, 4}
12 },
13 {
14 {-4, -5, 6, 9},
15 {2, 3}
16 }
17 };
18
19
20
21
22
1 for (int[][] array2D: test)
2 {
3 for (int[] array1D: array2D)
4 {
5 for(int item: array1D)
6 {
7 System.out.print(item + " ");
8 }
9 System.out.println();
10 }
11 System.out.println();
12 }
13 }
14 }
15
16
17
18
19
20
21
22
THANK YOU

You might also like