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 10 of 10

Thread: 4 dimensional array

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 4 dimensional array

    Hello everybody..

    My name is John and I have decided to learn java.

    I am fiddling around a bit with it and I want to make declare a 4dimensional int array where EACH array containing 20 cells.

    I have this:
    int[][][][] vierdeDimensie = new int[20][20][20][20];

    Is this correct?

    Thanks in advance!!!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 4 dimensional array

    Arrays contain elements. How many elements total do you need? As you've currently defined the array, it will hold 20 x 20 x 20 x 20 elements, and that's a lot. (But not a big deal for a computer.) What will you use this array for? Some part of a game?

  3. #3
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 4 dimensional array

    Quote Originally Posted by GregBrannon View Post
    Arrays contain elements. How many elements total do you need? As you've currently defined the array, it will hold 20 x 20 x 20 x 20 elements, and that's a lot. (But not a big deal for a computer.) What will you use this array for? Some part of a game?
    Thank you very much for replying.

    This is a practice exercise I am doing and the exercise literally says:
    Declare a 4 dimensional int array where EACH array containing 20 cells.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 4 dimensional array

    Then I'd say you nailed it.

  5. #5
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 4 dimensional array

    Thank you!!!!

    I tried some other stuff too. Like if I have a one dimensional array of 10 elements and I want to know content of the first position and last position of the string array.
    also I think I know how to get the length of the 3rd array.
    I came up with:
    String naam = namen[9];
    String naam = namen[0];
    int i = arrayvoorbeeld[0][0].length;

    Am I on the right track....?

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 4 dimensional array

    Taken together in the same scope, the first two lines would generate an error, but taken separately, the first line assigns the last or 10th element of namen to naam and the second line assigns the first element of namen to naam.

    4-dimensional arrays are hard for me to visualize, so when you say you want to find the length of the third array, I'd ask "Which one is the third array?" because I don't know.

    In a two-dimensional array, the question could be "How many columns are in the 8th row?" The answer to that would be:

    my2DArray[7].length

    That I can visualize and understand. 4-D arrays, forget it.

  7. #7
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 4 dimensional array

    Quote Originally Posted by GregBrannon View Post
    Taken together in the same scope, the first two lines would generate an error, but taken separately, the first line assigns the last or 10th element of namen to naam and the second line assigns the first element of namen to naam.

    4-dimensional arrays are hard for me to visualize, so when you say you want to find the length of the third array, I'd ask "Which one is the third array?" because I don't know.

    In a two-dimensional array, the question could be "How many columns are in the 8th row?" The answer to that would be:

    my2DArray[7].length

    That I can visualize and understand. 4-D arrays, forget it.
    Thank you for replying...

    I am doing an excersise to learn java and the exact question (they are all separate question) is:
    What command gives the length of the 3rd dimension of an array example.
    lets say I have the following 4 dimensional array:
    int[][][][] arrayvoorbeeld = new int[1][2][3][4];
    		int i = arrayvoorbeeld[0][0].length;

    The last line of code (int i = arrayvoorbeeld[0][0].length gives me the length of the 3rd array.

    Am I correct?

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 4 dimensional array

    Again, multi-dimensioned arrays greater than 3D are hard to visualize, so let's think this one through. It's important to use specific and precise language. I suspect English is not your native language, and that's fine, you're doing great, but there's a big difference between finding the length of the 3rd dimension and the length of the 3rd array. So let's stick to using "dimension" in this case. Now we have to agree on dimension numbering.

    If we were talking about a 2D array, the length of the name of the array, my2DArray.length, specifies the number of rows in my2DArray, so let's call that the 1st dimension. The number of columns in a specific row, or the 2nd dimension, is then found by my2DArray[row].length. Remember that multi-dimensioned arrays don't have to be square in Java. Each row could have a different length or different number of columns.

    Following that easier-to-understand pattern, then the length of the dimensions in your 4D array are:
    arrayvoorbeeld.length;            // length of the first dimension or the number of rows
    arrayvoorbeeld[x].length;           // length of the second dimension or the number of columns in row x
    arrayvoorbeeld[x][y].length;       // length of the third dimension or the depth of column (x, y)
    arrayvoorbeeld[x][y][z].length;   // length of the 4th dimension or the time of each depth (x, y, z)
    // (assuming the 4th dimension is time)
    In your case, everyone of the above for x, y, z = 0 through 19 should be 20, right? You might code that up to verify.

  9. #9
    Member
    Join Date
    Nov 2013
    Posts
    34
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 4 dimensional array

    Quote Originally Posted by GregBrannon View Post
    Again, multi-dimensioned arrays greater than 3D are hard to visualize, so let's think this one through. It's important to use specific and precise language. I suspect English is not your native language, and that's fine, you're doing great, but there's a big difference between finding the length of the 3rd dimension and the length of the 3rd array. So let's stick to using "dimension" in this case. Now we have to agree on dimension numbering.

    If we were talking about a 2D array, the length of the name of the array, my2DArray.length, specifies the number of rows in my2DArray, so let's call that the 1st dimension. The number of columns in a specific row, or the 2nd dimension, is then found by my2DArray[row].length. Remember that multi-dimensioned arrays don't have to be square in Java. Each row could have a different length or different number of columns.

    Following that easier-to-understand pattern, then the length of the dimensions in your 4D array are:
    arrayvoorbeeld.length;            // length of the first dimension or the number of rows
    arrayvoorbeeld[x].length;           // length of the second dimension or the number of columns in row x
    arrayvoorbeeld[x][y].length;       // length of the third dimension or the depth of column (x, y)
    arrayvoorbeeld[x][y][z].length;   // length of the 4th dimension or the time of each depth (x, y, z)
    // (assuming the 4th dimension is time)
    In your case, everyone of the above for x, y, z = 0 through 19 should be 20, right? You might code that up to verify.
    Hello,

    Thank you very much for your patience and your very detailed explanation!
    I meant length of the dimension indeed.

    You made it very clear for me.

    I am very motivated to continue and learn java.

    I will be back with questions, count on that ;-)

    Thanks a bunch

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 4 dimensional array

    You're welcome. Glad to help.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    bodylojohn (November 17th, 2013)

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. 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
  3. 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
  4. Help with looking through a two-dimensional array.
    By aesguitar in forum Algorithms & Recursion
    Replies: 14
    Last Post: June 1st, 2012, 11:10 AM
  5. Help with one-dimensional array
    By brandon66 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 26th, 2012, 04:59 PM