• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Multi dimensional array

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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];

Thanks in advance for your precious reply
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you please explain me / give diagramatic view of code

byte b2 [] [] [] [] = new byte [2][3][1][2];
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got the answer


[ May 05, 2008: Message edited by: Vishal Joshi ]
 
Vishal Joshi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final code for 3 dimensional array

yes i did it
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic