Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: How come the rows in a 2D array in Java are allowed vary in length?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default How come the rows in a 2D array in Java are allowed vary in length?

    From this tutorial,

    In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. Consequently, the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:

    Java Code:
    class MultiDimArrayDemo {
        public static void main(String[] args) {
            String[][] names = {
                {"Mr. ", "Mrs. ", "Ms. "},
                {"Smith", "Jones"}
            };
            // Mr. Smith
            System.out.println(names[0][0] + names[1][0]);
            // Ms. Jones
            System.out.println(names[0][2] + names[1][1]);
        }
    }
    The output from this program is:

    Java Code:
    Mr. Smith
    Ms. Jones

    1. I always thought that a multi-dimensional array is simply an array of arrays, like a 2 dimensional array is like a matrix where the rows represent arrays and the entire matrix represent the array containing those arrays which represent the rows.

      I can't think of another way in which there could be a multidimensional array. The question is that from the context of the quote above, how is a C 2-dimensional array different from a Java 2-dimensional array?

    2. In Java, arrays have fixed length. So in the case of a 2D array, The outer enclosing array (which contains other arrays) as well as the arrays contained (the rows) should have a fixed length. So what do they mean when they say that rows are allowed to vary in length?

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

    Default Re: How come the rows in a 2D array in Java are allowed vary in length?

    Quote Originally Posted by Zarah View Post
    I always thought that a multi-dimensional array is simply an array of arrays, like a 2 dimensional array is like a matrix where the rows represent arrays and the entire matrix represent the array containing those arrays which represent the rows.
    They are arrays of arrays. But they are not necessarily square or rectangular but ragged.

    So each array in the arrays of arrays can be of different length

    I can't think of another way in which there could be a multidimensional array. The question is that from the context of the quote above, how is a C 2-dimensional array different from a Java 2-dimensional array?
    C arrays are similar. A C array can be defined as a double ptr (e.g. char **foo). So you can assign different "array" lengths
    to the single array of pointers. In fact, an array of Strings in C is really a ragged array where each "string" is an array of characters terminated with a null (zero value). And as in Java, a "2D array" when fully defined with brackets and specific lengths is a square or rectangular array. But each "row" may be retrieved independently and treated as a "1D" array.

    In Java, arrays have fixed length. So in the case of a 2D array, The outer enclosing array (which contains other arrays) as well as the arrays contained (the rows) should have a fixed length. So what do they mean when they say that rows are allowed to vary in length?
    Probably related to your first question. The arrays are fixed length and can't change dynamically once allocated. But the array lengths of a each array in an array of arrays do not have to be the same. What I referred to as a ragged array.

    Regards,
    Jim
    Last edited by jim829; 04-22-2016 at 10:08 PM.
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: How come the rows in a 2D array in Java are allowed vary in length?

    @Jim829 This explanation clarified it for me. Thank you so very much.

Similar Threads

  1. Replies: 3
    Last Post: 04-08-2014, 10:24 PM
  2. Length of an Array: length() or just length ?
    By Jack_Tauson_Sr in forum New To Java
    Replies: 8
    Last Post: 03-18-2013, 08:16 PM
  3. Generic Array Creation - NOT ALLOWED?(java 5.0)
    By rkanagasikamani in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2010, 03:45 PM
  4. java Array.length property
    By X-ion in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 08:45 AM
  5. Int Array Allowed in main() Method?
    By Starclopsofish in forum New To Java
    Replies: 19
    Last Post: 10-03-2008, 04:22 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
  •