• Post Reply Bookmark Topic Watch Topic
  • New Topic

3D Array in Java Help  RSS feed

 
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working with datasets that contain x,y,z coordinates for each position (size = 40,40,40 for this instance).
My understanding is that java treats arrays differently to other languages like c etc, so if i need to access x data i need to use:



And to access y variables held in this dataset

Similarly for z it must be data[2][i][j]

Is my understanding correct?

Or is it more like [x][y][z] access? i.e.



There is a dearth of material available when i searched for multi-dimensional arrays so pardon me if this is too beginner a question. Thanks for looking,>
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use the same indexing scheme you used to put values into the array in the first place. That doesn't change. Note that Java does not have true multidimensional arrays. What you have declared is an array which has elements that are arrays which themselves have elements that are arrays. Think "Russian Nesting Dolls" and you'll have a good picture of how these kinds of arrays are organized in Java. However, for practical purposes, treating nested arrays in Java as you would a true multidimensional array in another language still works most of the time.
 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You would use the same indexing scheme you used to put values into the array in the first place. That doesn't change. Note that Java does not have true multidimensional arrays. What you have declared is an array which has elements that are arrays which themselves have elements that are arrays. Think "Russian Nesting Dolls" and you'll have a good picture of how these kinds of arrays are organized in Java. However, for practical purposes, treating nested arrays in Java as you would a true multidimensional array in another language still works most of the time.


Thanks for the reply. The data was read in by an external library whose source i cannot access, so i do not know how the data was read in.

So what is the difference between the two approaches i specified above? And which one is the right one? Thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess Russian Nesting Dolls might not give you the best picture. Imagine an egg tray which when you open it up, you find another tray where an egg would normally be. This repeats down into the last "dimension" where you will finally find the "eggs," which is of the type that you have associated with the array.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first index is the outermost array and the last index would be the innermost array. For simplicity, I'll refer to dimensions but as explained earlier, these are really levels of nesting. The first dimension is usually taken as the row and the second dimension as the column, if you had a two-dimensional structure. For three dimensions, the third index is the height/depth. That is, if you had data[][][], then the indexing scheme is usually data[x][y][z], so it's more like the second way you used. That's the way it's normally done. Since you do not know what indexing scheme was used to put data into the array, you can make this assumption first, test it, and make adjustments from the observed results if necessary.

Did that second snippet produce what you expected?
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Rosenberger uses the egg analogy for arrays. He says the individual elements are the eggs, the innermost arrays are boxes, the next level crates, the next level shelves the crates sit on, etc.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, it's dangerous to use data.length for all dimensions. In this case, it happens to work because the dimensions of all arrays are equal. However, nested arrays do not have to be the same length. The safest way to go about this would be to use constants for each dimension. To see this for yourself, try declaring data[20][20][10] and you'll get an exception if you use data.length in the innermost loop. data.length refers to the length of the outermost array.

Edit: actually, probably the most correct and safe way is to use the length of the current nested array you're iterating over, but that's really only necessary for the extreme case where you have ragged nested arrays. I'll leave that to you as an exercise to figure out. Just remember, nested arrays in Java may not necessarily be of the same length (ragged arrays).
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Fred Rosenberger uses the egg analogy for arrays. He says the individual elements are the eggs, the innermost arrays are boxes, the next level crates, the next level shelves the crates sit on, etc.

Yes, that makes more sense visually, rather than progressively smaller egg trays and finally, teeny-tiny eggs.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:BTW, it's dangerous to use data.length for all dimensions. . . .
But every array has a length field. If you use the conventional method for writing a for loop, with myArray.length in the middle (continuation), the for loop will traverse the array correctly, never mind how many elements it contains, and never throw an out of bounds exception:-
 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The first index is the outermost array and the last index would be the innermost array. For simplicity, I'll refer to dimensions but as explained earlier, these are really levels of nesting. The first dimension is usually taken as the row and the second dimension as the column, if you had a two-dimensional structure. For three dimensions, the third index is the height/depth. That is, if you had data[][][], then the indexing scheme is usually data[x][y][z], so it's more like the second way you used. That's the way it's normally done. Since you do not know what indexing scheme was used to put data into the array, you can make this assumption first, test it, and make adjustments from the observed results if necessary.

Did that second snippet produce what you expected?


Yes using 3 for loops and data[x][y][z] gives me 64000 values (40^3).
So assuming the second approach was sed for storing data,

I am trying to plot these points (in 2D) by fixing one plane.
For instance if i fix the z-plane to 1:


is this the correct way to access x and y variables from the data? Based on what you have said so far it seems this is incorrect as this is using the first approach.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That all depends on how the data is organized within these arrays. It could be the correct but without knowing more about the semantics of the indices and organization of the data within the data structure, I couldn't say one or the other. Most likely, however, it's not correct since you are only iterating though very thin "slices" of the data structure. That is, with this code you get the values of x from data[0][?][1] and the values of y from data[1][?][1]. This leaves a large portion of the 40x40x40 grid untouched. Is that how the data is organized? What do the values in the rest of the data structure represent then?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To help you visualize this, imagine a cube/grid with coordinates (0, 0, 0) at the bottom, front, left corner, and coordinates (39, 39, 39) at the top, rear, right corner. Draw a line from (0, 0, 1) to (0, 39, 1) -- the cells along that line hold all the x values that you will plot. Draw another line from (1, 0, 1) to (1, 39, 1) -- the cells along that line hold the y values that you will plot. Is that what you wanted to do?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevie Shorey wrote:is this the correct way to access x and y variables from the data? Based on what you have said so far it seems this is incorrect as this is using the first approach.

One thing that can help you an awful lot is to
(a) Use good names.
(b) Break up problems (and you clearly have a problem) into methods.

Take your last piece of code. I still have no idea what it's intended to do - it seems a very weird operation to me - but it plainly has some meaning for you. So NAME IT, and put it in a method that you can document, so that dummies like me can understand.

Methods can return arrays too.

Winston
 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Stevie Shorey wrote:is this the correct way to access x and y variables from the data? Based on what you have said so far it seems this is incorrect as this is using the first approach.

One thing that can help you an awful lot is to
(a) Use good names.
(b) Break up problems (and you clearly have a problem) into methods.

Take your last piece of code. I still have no idea what it's intended to do - it seems a very weird operation to me - but it plainly has some meaning for you. So NAME IT, and put it in a method that you can document, so that dummies like me can understand.

Methods can return arrays too.

Winston


I apologise for that. plotPixel() was just a pseudo-code representation. Should have elaborated. All it does is take x,y parameters and using jogl, draws a pixel.
 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That all depends on how the data is organized within these arrays. It could be the correct but without knowing more about the semantics of the indices and organization of the data within the data structure, I couldn't say one or the other. Most likely, however, it's not correct since you are only iterating though very thin "slices" of the data structure. That is, with this code you get the values of x from data[0][?][1] and the values of y from data[1][?][1]. This leaves a large portion of the 40x40x40 grid untouched. Is that how the data is organized? What do the values in the rest of the data structure represent then?


The way you phrased that made me realised, i have done it wrong definitely.

An 40x40x40 3-d model is stored in this array. What i am trying to do is,
1. fix one dimension (z in this instance) and plot ALL pixels in the x,y dimension for that plane.
2. using the value held in the array i.e. (data[0][1][zFixed]) make a color
3. plot the pixel using this color.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the array element represents a color value and its coordinates within the array represent a spatial coordinate. That is, data[1][1][1] should be plotted in space at coordinates (1, 1, 1) with a pixel of the color represented by the element data[1][1][1]. Correct?

In that case, if you were plotting one plane, then you would fix Z and iterate normally through data[x][y]. That is, x == i, y == j, and color == data[i][j][Z]. What I don't get is the fact that plotPixel only takes the spatial coordinates and no information about the color of the pixel to plot. Maybe I missed something?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevie Shorey wrote:My understanding is that java treats arrays differently to other languages

I think maybe you're getting too hung up on this thought ...

The only difference in Java is that what would be "multi-dimensional" arrays in other languages are really nested arrays in Java -- conceptually, you can still treat nested array structures pretty much the same way you treat multi-dimensional arrays in other languages.
 
Stevie Shorey
Ranch Hand
Posts: 45
Android Chrome Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for taking time to answer my questions Junilu

No i do not use the indice values as coordinates, but the values inside the array.
I am looking at it from a different view:
i need to plot every pixel! I shade diffferent pixels differently based on the value contained in the array (using a color mapping).

I will try this out, im sure it will take me some time, and report back the progress.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!