• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java code for 3Dimensional Array

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think "info[k].length" should read "info[i].length" instead.
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags next time.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajendra Prakash
Ranch Hand
Posts: 293
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say, isn't this the same problem as Java code to print 3 * 3 matrix?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic