Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java arrays

Posted on 2012-03-24
5
Medium Priority
?
391 Views
Last Modified: 2012-03-24
I'm trying to learn java, and the book I'm reading is now describing arrays. I get the 2d arrays pretty good, so if the code that describes the array looks something like this

int array2d [][] = {
  { 34,50,59 },
  { 40,66,70 },
  { 22,44,88 }
} ;

then I can picture that the array would be a grid that looks like this

grid
but then the book throws 3d arrays at me, and doesn't describe it well. I was guessing that the 2d array, is like a square and a 3d array is like a cube, and if that is the case, i'm having a hard time with how the elements of the array are "addressed". For instance, in my 2d array above, the element at array[1][2] is the number 70.

and something like

System.out.println( array2d [1][2] ) ;

should show the number 70.

but for a 3d array like this

int array3d[][][] = {
  { {3,5,7} , {4,7,9} , {1,3,5},
  { {4,8,0} , {3,6,9} , {1,3,8},
  { {3,7,9} , {2,4,1} , {3,2,9}
} ;

I'm not even sure how the numbers are "addressed" let alone how I would make a

System.out.println

statement which would display the numbers I want. For instance, would the last 9 in the last column have an "address" of

System.out.println ( array3d [2] [2] [2] ) ;
0
Comment
Question by:JeffBeall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37760978
Yes your last nine will be element array3d[2][2][2]
But check - I think tyou need more closing braces - there is mistake in your definition of 3d array _ you need to close two more brace
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37760990
Thisn of 3d array as a say oone d-imensional array where ewach element is two diensuional array and it will become simplee

={

 {       } //  each of these 2 d-array, so is suold be {    { 1,2,3  }. {4,5,6   }.,  {4,8,9   }  }

{       } // here also

{       }  // here also

   }


In gerenral don't be very much upset - it is difficult to understand - that's why no one normally usee more than 2d -arrays in real code, so don't worry too much
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 37760993
Good indentation will help you

        int[][][] array3d = {
                {
                    { 3, 5, 7 },
                    { 4, 7, 9 },
                    { 1, 3, 5 }
                },
                {
                    { 4, 8, 0 },
                    { 3, 6, 9 },
                    { 1, 3, 8 }
                },
                {
                    { 3, 7, 9 },
                    { 2, 4, 1 },
                    { 3, 2, 9 }
                }
            };

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 1000 total points
ID: 37760998
so in the picture I drew above

={

 {       } //  each of these 2 d-array, so is suold be {    { 1,2,3  }. {4,5,6   }.,  {4,8,9   }  }

{       } // here also

{       }  // here also

   }


array3d[0]  is a 2d array:  {    { 1,2,3  }. {4,5,6   }.,  {4,8,9   }  }


then array3d[0][1]  is a one-d array  {4,5,6   }.

and array3d[0][1][2] = 6
0
 
LVL 1

Author Closing Comment

by:JeffBeall
ID: 37761219
Ok, I think I get it now, it helped to know that my guess was correct. Thanks for the help.
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question