Three dimensional array in Java Help

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:

Code :

// i have already read in the values from the dataset file to an int[40][40][40] array called data.
for (i=0;i<int.length;i++){
for (j=0;i<int[i].length;j++){
out.println("x data = "+ data[0][i][j];
}
}

And to access y variables held in this dataset

Code :

for (i=0;i<int.length;i++){
for (j=0;i<int[i].length;j++){
out.println("x data = "+ data[1][i][j];
}
}

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.

Code :

for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data.length; j++) {
for (int k = 0; k < data.length; k++) {
out.println("{" + i + ", " + j + ", " + k + "}=\t" + data[i][j][k]);
count++;
}
}
}

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,>

Re: Three dimensional array in Java Help

Quote:

Originally Posted by

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

This is how it would work

Re: Three dimensional array in Java Help

Re: Three dimensional array in Java Help

Your code says that there are 3 two dim arrays in the three dim array. The first dim is for x,y,z

x,y and z each have a 2 dim array to hold their data.

What data is stored in those two dim arrays?

Quote:

contain x,y,z coordinates for each position

I'd think you'd use a 2 dim array.

Each row would be a position

Each row would have 3 columns with the values for x,y,z

Re: Three dimensional array in Java Help

Interesting take Norm. I guess we need the question answered...

Are you looking for "an array to hold x,y,z positions" or "a 3D array holding data at position x,y,z"?

Re: Three dimensional array in Java Help

Quote:

Originally Posted by

**Chris.Brown.SPE**
Interesting take Norm. I guess we need the question answered...

Are you looking for "an array to hold x,y,z positions" or "a 3D array holding data at position x,y,z"?

Hi Chris, I need to hold data about a 3D image. So needs to hold x,y,z positions about each pixel.

Re: Three dimensional array in Java Help

That sounds more like what Norm was talking about. One big array where each position represents one pixel then each has a small array of x,y,z coordinates.

Do they need to be in a particular order?

Another way to do it would be to create your own object that holds x,y,z coordinates then create an array of them.

Re: Three dimensional array in Java Help

The data is loaded into an int[][][] array by an external .jar library whose source i do not have so i cannot do any other way but this.

To correct you each position only holds one scalar value. i.e.

data[1][1][0] = 5

This is where it gets confusing. The 5 above is the colour of the position which is used to colour that pixel.

Re: Three dimensional array in Java Help

Once the data is loaded into the array, what does the program use it for?

Re: Three dimensional array in Java Help

So the array itself is a representation of the image. One array dimension for each dimension of the image and the value is the color of the pixes.

This will work as i stated in my original post. data[x-axis][y-axis][z-axis]

Re: Three dimensional array in Java Help

Quote:

Originally Posted by

**Norm**
Once the data is loaded into the array, what does the program use it for?

I am using it to draw an represent a 3-D image. so i will plot each pixel manually using the data in the array.

Another use of the the scalar data contained in each position will be to use it to set the colour of the pixel.

--- Update ---

Quote:

Originally Posted by

**Chris.Brown.SPE**
So the array itself is a representation of the image. One array dimension for each dimension of the image and the value is the color of the pixes.

This will work as i stated in my original post. data[x-axis][y-axis][z-axis]

The problem is extracting individual values for x and y (since you need both to plot the image). I am only plotting in two dimensions.

So if i wanted to plot all pixels in z=1 dimension, how do i extract all x and y values?

i.e. all combinations of data[x][y][5] where x and y are indices.

Re: Three dimensional array in Java Help

If you set one of the dimensions (z) to a constant value (5), there would be 40X40 values in the other two dimemsions

Re: Three dimensional array in Java Help

Quote:

Originally Posted by

**Norm**
If you set one of the dimensions (z) to a constant value (5), there would be 40X40 values in the other two dimemsions

Hi,

That bit i understand. What i dont understand is getting seperate x and y values since i need them in JOGL to plot this pixel.

For instance

data[1][0][5]

data[1][1][5]

data[1][2][5]

will give me data held at index 1,0; 1,1; 1,2 (for fixed z-dim 5)

Note these are indices not x,y coordinates.

At each of these locations, a double value is held which determines the colour of the pixel.

This is slightly confusing as indices and coordinates are seperate in this question.

Re: Three dimensional array in Java Help

Quote:

Note these are indices not x,y coordinates.

Can you explain the difference between indices and coordinates? They are usually the same thing.

Re: Three dimensional array in Java Help

Quote:

Originally Posted by

**shadysback**
This is slightly confusing as indices and coordinates are seperate in this question.

Why do indices and coordinates have to be different? Just use the x,y coordinates as your indices. Unless there is some issue like your coordinates can be negative or something.

Re: Three dimensional array in Java Help

After much tweaking and deliberation, i managed to fix it. Both of you were right! The indices were indeed coordinates. The value held at each index location represented the colour of the pixel.

First setting the color, then plotting the pixel using an iteration for all possible indexes, i was able to model the image. Thanks once again for your help.

I just made a mountain out of a molehill.