Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Three dimensional array in Java Help

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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:


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

    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.

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


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Three dimensional array in Java Help

    Quote Originally Posted by shadysback View Post
    Or is it more like [x][y][z] access? i.e.
    This is how it would work
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Three dimensional array in Java Help


  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?

    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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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"?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  6. #6
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Three dimensional array in Java Help

    Quote Originally Posted by Chris.Brown.SPE View Post
    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.

  7. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Three dimensional array in Java Help

    Once the data is loaded into the array, what does the program use it for?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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]
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  11. #11
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Three dimensional array in Java Help

    Quote Originally Posted by Norm View Post
    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 View Post
    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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Three dimensional array in Java Help

    Quote Originally Posted by Norm View Post
    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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Three dimensional array in Java Help

    Note these are indices not x,y coordinates.
    Can you explain the difference between indices and coordinates? They are usually the same thing.
    If you don't understand my answer, don't ignore it, ask a question.

  15. The Following User Says Thank You to Norm For This Useful Post:

    shadysback (April 12th, 2013)

  16. #15
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: Three dimensional array in Java Help

    Quote Originally Posted by shadysback View Post
    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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  17. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    shadysback (April 12th, 2013)

  18. #16
    Junior Member
    Join Date
    Oct 2012
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Create two dimensional array from a one dimensional array
    By Kristenw17 in forum Collections and Generics
    Replies: 1
    Last Post: April 9th, 2013, 07:51 PM
  2. Java (2 dimensional array problem)
    By JoshuAtenista in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 21st, 2013, 06:38 AM
  3. need two dimensional array help..
    By mmagyar14 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2013, 09:11 PM
  4. Converting Two dimensional array into an Array list
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 11
    Last Post: September 29th, 2012, 04:23 PM
  5. Help with looking through a two-dimensional array.
    By aesguitar in forum Algorithms & Recursion
    Replies: 14
    Last Post: June 1st, 2012, 11:10 AM