programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Jeanne Boyarsky
• Tim Cooke
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Mikalai Zaikin
• Carey Brown
Bartenders:

# Multi dimensional array

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
hi friends,
I am bit confuzed with multi dimensional array
if i write
byte [] [] b = new byte [2][1];

it means i have created 2 arrays , each has a capacity of 1 element
then what will be under the case of

byte [] [] b = new byte [1][2];

Ranch Hand
Posts: 147
• Number of slices to send:
Optional 'thank-you' note:
Hi Vishal,

Here you are creating array b having only one element, and that element is actually one array having two elements.

Vishal Joshi
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
can you please explain me / give diagramatic view of code

byte b2 [] [] [] [] = new byte [2][3][1][2];

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
Hi Vishal,

byte [][] b = new byte[2][1];

the code simply means that you are creating a single double-dimension array, having 2 rows, with one element each(and not 2 different arrays). Think of a 2 dimensional array(in this case) as a 2*1 matrix.

Thanks.
You can refer to the core SCJP concepts in more coarse level at:
SCJP Coarse Concepts
[ May 05, 2008: Message edited by: Devesh Chanchlani ]

Jart Bo
Ranch Hand
Posts: 147
• Number of slices to send:
Optional 'thank-you' note:
Think of it this way:

You've created one big square-shaped box named b, inside b are 2 smaller boxes..

Vishal Joshi
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
i tried the following code and i got 3840 lines of output

I need only elements of my array to get displayed only once

public class tp
{
public static void main(String [] args)
{
byte [] [] b = new byte [2] [1];
b [0][0] =1;
b[1][0] = 2;
System.out.println(b[0][0]);
System.out.println(b[1][0]);

int [][][] j = new int [4][3][2];

j [0][0][0] = 000;j [0][0][1] = 001;
j [0][1][0] = 010;j [0][1][1] = 011;
j [0][2][0] = 020;j [0][2][1] = 021;

j [1][0][0] = 100;j [1][0][1] = 101;
j [1][1][0] = 110;j [1][1][1] = 111;
j [1][2][0] = 120;j [1][2][1] = 121;

j [2][0][0] = 200;j [2][0][1] = 201;
j [2][1][0] = 210;j [2][1][1] = 211;
j [2][2][0] = 220;j [2][2][1] = 221;

j [3][0][0] = 300;j [3][0][1] = 301;
j [3][1][0] = 310;j [3][1][1] = 311;
j [3][2][0] = 320;j [3][2][1] = 321;

System.out.println(j[3][0][1]);
int jw =0;
for (int j1=0 ;j1<j.length ;j1++ )
{
for(int j2=0 ; j2 < j[j1].length;j2++)
{
for (int j3=0; j3 < j[j1][j2].length ;j3++ )
{
for (int j4=0; j4 < j[j1][j2][j3] ; j4++)
{
System.out.println(j[j1][j2][j3]);
jw++;
}

}
}
}
System.out.println(""+jw);
}
}

Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
(a) please use code tags, multi-nested loops are impossible to read (and understand) if not formatted.

(b) Why are you doing the for loop with j4? In that loop, you are actually counting up to the value in the array, not to the array's length. Your array has 3 dimension, so 3 nested loops should do for printing out all the elements.

Vishal Joshi
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
How to print the following assigned array using for loop

int [][][] j = new int [4][3][2];

j [0][0][0] = 000;j [0][0][1] = 001;
j [0][1][0] = 010;j [0][1][1] = 011;
j [0][2][0] = 020;j [0][2][1] = 021;

j [1][0][0] = 100;j [1][0][1] = 101;
j [1][1][0] = 110;j [1][1][1] = 111;
j [1][2][0] = 120;j [1][2][1] = 121;

j [2][0][0] = 200;j [2][0][1] = 201;
j [2][1][0] = 210;j [2][1][1] = 211;
j [2][2][0] = 220;j [2][2][1] = 221;

j [3][0][0] = 300;j [3][0][1] = 301;
j [3][1][0] = 310;j [3][1][1] = 311;
j [3][2][0] = 320;j [3][2][1] = 321;

Vishal Joshi
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:

[ May 05, 2008: Message edited by: Vishal Joshi ]

Vishal Joshi
Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
Final code for 3 dimensional array

yes i did it

 Paddy spent all of his days in the O'Furniture back yard with this tiny ad: a bit of art, as a gift, that will fit in a stocking https://gardener-gift.com