#30 jagged and 3D Array in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)What if array of array of different size ?
2)Jagged array concept
3)If i donot know column size of array
4)traverse the array using for loop and enhance loop

#1
Suppose we want to create a 2d dimensional array such that it has
5 rows but we donot know for each row we need different size for column.
for that we go for array of array of different size

-------------
| 1 | 2 | 3 |
---------------------
| 4 | 5 | 6 | 7 | 8 |
-------------------------
| 1 | 5 | 9 | 2 | 4 | 7 |
-------------------------

in above diagram we have three rows and first row has 3 elements
second row has 5 elements and third row has 6 elements.

#2#3
jagged array concept come in java 8
-- Jagged array is a multidimensional array where member arrays are of different size. For example, we can
create a 2D array where first array is of 3 elements, and is of 4 elements. Following is the example demonstrating
the concept of jagged array.

syntax to create --

int nums[][]=new int[3][];
nums[0] =new int[3];
nums[1] =new int[5];
nums[2] =new int[6];

initalize value in array using random()method present in Math class
for(int j=0;j nums[i].length;j++){
}
}
#5
Traverse using for loop :
for(int j=0;j nums[i].length;j++){
}
}

Traverse using enhanced for loop:
for(int x[]:nums){
for(int y:x){
}
}

Note: Just like 2-d jagged array we can create 3-d , 4-d jagged array
for cursoity:
int num[][][]=new int[3][][];
num[0]=new int[2][];
num[1]=new int[3][];
num[2]=new int[4][];
num[0][0]=new int[2];
num[0][1]=new int[3];
num[1][0]=new int[4];
num[1][1]=new int[5];
num[1][2]=new int[6];
num[2][0]=new int[7];
num[2][1]=new int[8];
num[2][2]=new int[9];
num[2][3]=new int[10];

Intialize the array:
for(int j=0;j num[i].length;j++){
for(int k=0;k num[i][j].length;k++){
}
}
}

Traverse the element of array:
for(int j=0;j num[i].length;j++){
for(int k=0;k num[i][j].length;k++){
}
}
}

More Learning :

Donation:
PayPal Id : navinreddy20
Рекомендации по теме
Комментарии
Автор

had a hard time finding a proper video on jagged array and how to update it . thanks bro


ayashazhar
Автор

You are amazing Telusku, the way you have explained the concept of Array, You deserve appreciation.You nailed it bro👏❣

sherislam
Автор

In first for loop instead of i<nums.length we can write the no of rows ( I.e 3) bcoz here no of rows are fixed.

DevilGaming-blog
Автор

I like the way you explain Boss, thanks 😊😊

IshmaelSmart-lv
Автор

Can we do jagged array of 2nd dimension in 3 dimensional array?

balapraveen
Автор

i had tried much more complex one three arrays
class demo
{
public static void main(String[] args)
{
int num[][][]= new int[3][2][];
num[0][0]= new int[10];
num[0][1]= new int[6];
num[1][0]= new int[2];
num[1][1]= new int [5];
num[2][0]=new int[8];
num[2][1]=new int[8];

for(int i=0;i<num.length;i++)
{
for (int j = 0; j <num[i].length; j++)
{
for (int k=0; k<num[i][j].length;k++)
{
num[i][j][k]= (int)(int)(Math.random() * 100);
}


}

}




for(int n[][]:num)
{
for(int m[]:n)
{
for(int l:m)
{
System.out.print(l+" ");
}
System.out.println();
}
}
}
}

and i got this out put

50 6 45 32 72 96 7 49 39 51
64 4 11 78 79 77
87 43
97 54 40 59 82
86 28 31 67 62 6 51 92
13 34 64 59 32 36 17 46

SowjanyaKadiveti
Автор

hii, how can we assign fixed values of our own to arrays with different sizes,

anilkumargangarapu
Автор

Three dimensional array with proper example...i couldnt understand this one.

AdhilMhmdVK
Автор

// Online Java Compiler
// Use this editor to write, compile and run your Java code online
public class Demo {

public static void main(String[] args)

{


int nums[][][]=new int [3][4][5]; // three dimensional

for(int i=0;i<nums.length;i++)
{
for(int j=0;j<nums[i].length;j++)
{
for(int k=0;k<nums[i][j].length;k++){

}
}

}
for(int i=0;i<nums.length;i++){
for(int j=0;j<nums[i].length;j++){
for(int k=0;k<nums[i][j].length;k++){
System.out.print(nums[i][j]+" ");
}
}

}
}
}
This is the code for 3D array...

purnimanaskar
Автор

public class Array {
public static void main(String[] args) {
int num1[]=new int[3];
int num[][]=new int[3][4];
for(int i=0;i<3;i++){
num1[i] = (int) (Math.random() * 10);
for(int j=0;j<4;j++){

}
}
System.out.println("\nTwo dimensitonal Array");

for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(num[i][j]+" ");
}
System.out.println();
}
System.out.println("One dimensitonal Array");
for(int n:num1){
System.out.print(n+" ");
}
System.out.println("\nTwo dimensitonal Array");
for(int n[]:num){
for(int m:n)
System.out.print(m+" ");
System.out.println();
}
int num2[][]=new int[4][];//jagged array
num2[0]=new int[2];
num2[1]=new int[3];
num2[2]=new int[4];
num2[3] = new int[5];
for(int i=0;i<4;i++){
for(int j=0;j<num2[i].length;j++){

}
}
System.out.println("Jagged Array");
for (int n2[] : num2) {
for (int m2 : n2) {
System.out.print(m2+" ");
}
System.out.println();
}
System.out.println("3D Array");
int num3[][][]=new int[3][2][2];
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
for(int k=0;k<2;k++){
num3[i][j][k]= (int) (Math.random() * 10);

}
}
}
System.out.println();
for(int n3[][]:num3){
for(int n31[]:n3){
for(int n311:n31)
System.out.print(n311+" ");
}
System.out.println();
}

}
}

RameshMaity
Автор

Why are you not updating VS Code? Please update VS Code.

puruagni
Автор

This video cant understand, where does math & *10 come from!

thelivereactions