• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing a 3d Array of ints

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arr[x][y][z]
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic