This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Accessing a 3d Array of ints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing a 3d Array of ints" Watch "Accessing a 3d Array of ints" New topic
Author

Accessing a 3d Array of ints

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have a 3 dimensional array if ints but i am drawing a blank on how to access individual values.
I plan to plot them in 2d planes so i want to be able to extract all the x values, all the y values and all the z values to achieve this.
I know in a normal array you can access elements by the index so array[1] but the fact its 3 dimensional is confusing me slightly.
So if anyone could help me to extract all of the x y and z co ordinates that would be great.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

arr[x][y][z]
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Yes im aware that they will be stored [x][y][z] but i am asking how do i access them and pull them from this array.

Say i want to do the x-y plane first, could someone explain how i would get the values i want from the 3d array? is it a case of keeping the y and z values the same an getting each x value, then increment y and z by one and get the next x values? I know the dimensions of the dataset if that helps as well.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

tom davies wrote:Yes im aware that they will be stored [x][y][z] but i am asking how do i access them and pull them from this array.

Say i want to do the x-y plane first, could someone explain how i would get the values i want from the 3d array? is it a case of keeping the y and z values the same an getting each x value, then increment y and z by one and get the next x values? I know the dimensions of the dataset if that helps as well.


I'm assuming you mean you want to iterate the array, as in go through each item. First, perhaps it would be easier for you to start off with a 2D array and then go from there. You're right about keeping one value constant, so you'd want

x y
arr[0][0]
arr[0][1]
arr[0][2]

So you're incrementing y, while keeping x constant. Hint: Use two for loops.







tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Paul Mrozik wrote:
tom davies wrote:Yes im aware that they will be stored [x][y][z] but i am asking how do i access them and pull them from this array.

Say i want to do the x-y plane first, could someone explain how i would get the values i want from the 3d array? is it a case of keeping the y and z values the same an getting each x value, then increment y and z by one and get the next x values? I know the dimensions of the dataset if that helps as well.


I'm assuming you mean you want to iterate the array, as in go through each item. First, perhaps it would be easier for you to start off with a 2D array and then go from there. You're right about keeping one value constant, so you'd want

x y
arr[0][0]
arr[0][1]
arr[0][2]

So you're incrementing y, while keeping x constant. Hint: Use two for loops.


Thanks. For a 2d array if im looking to extract all the x values would i be looking at something like this.



A mix of pseudo in there but i think you get the idea.
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

That looks about right, though I'd probably do the reverse in terms of variable names:




So now you need to take it one step further, for a 3d array, which means you need to go a little deeper for the z and you'll need x and y to be constant.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Paul Mrozik wrote:That looks about right, though I'd probably do the reverse in terms of variable names:




So now you need to take it one step further, for a 3d array, which means you need to go a little deeper for the z and you'll need x and y to be constant.


So for the 3D array, if i am still looking at the x-y plane i could have the same two for loops as above but i keep z the same throughout. Then increase z to get the next slice etc?
I think understand it better now, Thank you!
Paul Mrozik
Ranch Hand

Joined: Feb 10, 2013
Posts: 117

Okay, wait, I think I missed something here. You need actual x values, y values, and z values. In that case, I'd recommend creating a separate class, something like:



You can then extract x and y to plot your plane.


tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Paul Mrozik wrote:Okay, wait, I think I missed something here. You need actual x values, y values, and z values. In that case, I'd recommend creating a totally separate object, something like:



You can then extract x and y to plot your plane.




I have created an ArrayList of co ordinates and then i loop through and plot them. It seems to be working in all 3 planes this way.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11255
    
  16

I hate to tell you this, but you don't have a 3-D array. The Java language makes no such provision for multi-dimensional arrays.

What you have is a one dimensional array that can hold objects. Those objects happen to be...wait for it...arrays, and THOSE happen to hold...arrays.

In a true 3-d array, you can't say myArray[2]. That doesn't make sense. But in java, that gets you a real object. Further, this allows for what is known as 'ragged arrays'. The 0th element may hold 5 things, but the 1st may hold ten, etc.

It is a subtle, but important, difference.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Accessing a 3d Array of ints