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

1. Senior Member
Join Date
Mar 2014
Posts
286
Rep Power
6

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

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

Originally Posted by Zarah
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.

3. Senior Member
Join Date
Mar 2014
Posts
286
Rep Power
6

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

#### Posting Permissions

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