Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By gimbal2

Thread: Multi Dimensional Arrays

  1. #1
    Join Date
    Jul 2015
    Posts
    5
    Rep Power
    0

    Default Multi Dimensional Arrays

    Can someone explain multi dimensional arrays to me in simple terms please.(ps i am very new to programming and java).

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Multi Dimensional Arrays

    Java doesn't have multidimensional arrays (it does allow arrays of arrays).

    Do you understand arrays? Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Each element of an array can also be an array.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Multi Dimensional Arrays

    when each element in an array is an array, isn't that called a Multi-Dimensional Array?

    ("A multidimensional array is actually an array in which each element is another array." is a literal quote from the book "Introduction to Java Programming Comprehensive 8th Edition" by Y. Daniel Liang)

    P.S. not trying to step on your toes but if you say that Java doesn't have multi dimensional arrays I'd like to understand what you mean better.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Multi Dimensional Arrays

    Quote Originally Posted by Artemia View Post
    when each element in an array is an array, isn't that called a Multi-Dimensional Array?

    ("A multidimensional array is actually an array in which each element is another array." is a literal quote from the book "Introduction to Java Programming Comprehensive 8th Edition" by Y. Daniel Liang)

    P.S. not trying to step on your toes but if you say that Java doesn't have multi dimensional arrays I'd like to understand what you mean better.
    I interpret that description of explaining how you construct an "n-dimensional array" in java. Most folks use the term "multi-dimensional arrays" in Java to simplify discussion. Internally, Java does not have multidimensional arrays. Only single-d arrays which may hold objects or primitives (the objects may be other arrays and those arrays may hold other arrays, etc). It has been my experience that real multi-d arrays have internal structure and their behavior and how they are used is enforced by the compiler. Java does not really do that but does permit great flexibility in how they can be used.

    Think about this syntax (which you don't see too much).

    Java Code:
    int [] nums[];
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Multi Dimensional Arrays

    I see, that makes it easier to understand.
    I never thought of multi-dimensional arrays as a whole separate class of array like that.

    thanks for clarifying

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Multi Dimensional Arrays

    Quote Originally Posted by Artemia View Post
    I see, that makes it easier to understand.
    I never thought of multi-dimensional arrays as a whole separate class of array like that.

    thanks for clarifying
    Here is another example (quite ugly actually). Object is simply a single dimension array. The [][] syntax that you normally see just makes accessing the information easier.

    Java Code:
          Object[] ints = new Object[3];
          ints[0] = new int[] {1,2,3};
          ints[1] = new int[] {4,5,6,7};
          ints[2] = new int[] {8,9,10,11,12};
          
          for (int j = 0; j < ints.length;j++) {
             int len = ((int[])ints[j]).length; 
             for (int k = 0; k < len; k++) {
                System.out.print(((int[])ints[j])[k] + " ");
             }
             System.out.println();
          }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Multi Dimensional Arrays

    Quote Originally Posted by jim829 View Post
    Think about this syntax (which you don't see too much).

    Java Code:
    int [] nums[];
    My eyes! my eyes!!

    db

    -- some similar discussion here: What is Object[][] (Beginning Java forum at JavaRanch)
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Multi Dimensional Arrays

    [] []
    Quote Originally Posted by DarrylBurke View Post
    My eyes! my eyes!!
    If your eyes are square like that you need to take a vacation from computer and TV screens for a while.
    DarrylBurke likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Multi-Dimensional arrays [SOLVED]
    By iLearnJava in forum New To Java
    Replies: 1
    Last Post: 12-05-2014, 01:46 PM
  2. Replies: 4
    Last Post: 06-28-2014, 05:00 PM
  3. Replies: 4
    Last Post: 08-01-2010, 09:25 AM
  4. Multi-dimensional arrays
    By Implode in forum New To Java
    Replies: 1
    Last Post: 09-15-2009, 08:50 AM
  5. [SOLVED] Multi-dimensional arrays
    By thelinuxguy in forum Advanced Java
    Replies: 3
    Last Post: 05-07-2009, 02:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •