Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login

# Multi dimensional array

Vishal Joshi
Greenhorn

Joined: Apr 21, 2008
Posts: 9
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];

In the Middle of Difficulty Lies Opportunity.
Jart Bo
Ranch Hand

Joined: Oct 22, 2007
Posts: 144
Hi Vishal,

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

SCJP, SCWCD
Vishal Joshi
Greenhorn

Joined: Apr 21, 2008
Posts: 9
can you please explain me / give diagramatic view of code

byte b2 [] [] [] [] = new byte [2][3][1][2];
Devesh Chanchlani
Greenhorn

Joined: Sep 04, 2007
Posts: 19
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

Joined: Oct 22, 2007
Posts: 144
Think of it this way:

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

Joined: Apr 21, 2008
Posts: 9
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);
}
}
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
(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

Joined: Apr 21, 2008
Posts: 9
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

Joined: Apr 21, 2008
Posts: 9

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

Joined: Apr 21, 2008
Posts: 9
Final code for 3 dimensional array

yes i did it

I agree. Here's the link: http://aspose.com/file-tools

subject: Multi dimensional array