Hi Yaron,
Thanks for helping out. I did what you suggested and this is my code now:
. . . .
int[][][] myInt = new int[2][2][2];
int myNum1 = 1000;
for (int i = 0; i < myInt.length; i++) {
myNum1 = myNum1 + i;
for (int j = 0; j < myInt[i].length; j++) {
myNum1 = myNum1 + i + j;
for (int k = 0; k < myInt[i][j].length; k++) {
myInt[i][j][k] = myNum1 + i + j + k;
System.out.print(myInt[i][j][k] + " ");
. . .
the output is 1000 1001 1002 1003 1004 1005 1007 1008, which is correct
but when I change the size of the array , say like myInt[3][2][3] i get
this output; 1000 1001 1002 1003 1004 1005 1007 1008 1011 1012 1015 101.
Where do you the mistake was?
Yaron Jackoby wrote:
> Hi,
>
> You should insert the value to the array like: myInt[i][j][k] = myNum1;
> the loop will take care to set in all the array.
>
>
> BR,
> Yaron
>
> On Mon, Jun 29, 2009 at 2:19 PM, Bong <[email protected]
> <mailto:[email protected]>> wrote:
>
>
> Hi,
>
> From Lesson-1036 Java Array,
>
> 4. (For your own exercise) Create a NetBeans project as following.
> Build and run the program.
>
> * Declare and initialize 3-dimensional array of int
> * Initialize each cell with increasing integer number starting
> from 1000, 1001, 1002, and so on.
>
> can someone possibly show me the solution to the said exercise. I've
> been trying it out but i don't seem to understand how a 3-dimensional
> array is arranged.
>
> Here's my code:
>
> public class JavaThreeDimensionArray {
>
> // Creates a new instance of JavaThreeDimensionArray
> public JavaThreeDimensionArray() {
> }
>
> public static void main(String[] args) {
>
> // Declare and create a three-dimentional int array whose size
> is
> // 5 by 2 by 2
> int[][][] myInt = new int[2][2][2];
> int myNum1 = 1000;
>
> for (int i = 0; i < myInt.length; i++) {
> for (int j = 0; j < myInt[i].length; j++) {
> for (int k = 0; k < myInt[i][j].length; k++) {
> myNum1 = myNum1 + i + j + k;
> System.out.print(myNum1 + " ");
> }
> }
> }
> }
>
> And here's my output:
> 1000 1001 1002 1004 1005 1007 1009 1012
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---