aspose file tools*
The moose likes Beginning Java and the fly likes Java code for 3Dimensional Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java code for 3Dimensional Array" Watch "Java code for 3Dimensional Array" New topic
Author

Java code for 3Dimensional Array

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
I need code for 3 dimensional array . The following code throws ArrayIndexOutofBounds Exception.
change return type as integer array not string array. I am new to array concepts.need to print 3 rows and 3 columns

String info[][][] = new String[3][4][5];
for (i=0; i<info.length ; i++)
{
for (j=0 ; j<=3; j++)
{
for(k=0;k<=info[k].length;k++)
{
info[i][j][k] = "String[" + i + "," + j + "," + k + "]";

}
}
}
System.out.println(info[i][j][k]);
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41905
    
  63
I think "info[k].length" should read "info[i].length" instead.


Ping & DNS - my free Android networking tools app
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
From the following code , i need to print all 3 s like 3 3 3
3 3 3
3 3 3
int[][] a2 = new int[3][3];
for (i=0; i<a2.length; i++)
{
for (j=0; j<a2[i].length; j++)
{
a2[i][j]=i;
System.out.print(" " + a2[i][j]);

}
System.out.println("");
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Please UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

Your code was nearly right, but you weren't finding the lengths correctly, wouldn't something like:



He was so close, so I apologise if anyone thinks I am giving out the answer.

Sean


I love this place!
Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
3 3 3
3 3 3
3 3 3
i need to print all 3 s like

[code]

int[][] a2 = new int[3][3];
for (i=0; i<a2.length; i++)
{
for (j=0; j<a2[i].length; j++)
{
a2[i][j]=i;
System.out.print(" " + a2[i][j]);

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

[code]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Say, isn't this the same problem as Java code to print 3 * 3 matrix?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Sean Clark wrote:He was so close, so I apologise if anyone thinks I am giving out the answer.

There's no ambiguity, you gave out the answer. Consider next time dropping a hint, like "what's the highest allowable index for an array of size n" or something similar--the more thought process involved the more retention, and understanding, there is. When the answer is given out, there's less thought involved, even though the solution is the same.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Java code for 3Dimensional Array