1

What are the differences between multidimensional arrays and array-of-arrays?

Why Java supports arrays of arrays, rather than multidimensional arrays ?

10
  • 2
    There are no multidimensional arrays, so your question doesn't make sense. Commented Feb 28, 2015 at 13:41
  • 1
    you are right @chrylis .. Why Java supports arrays of arrays, rather than multidimensional arrays ? is my main question Commented Feb 28, 2015 at 13:42
  • 1
    Isn't a multidimensional array just a special case of array of arrays? Commented Feb 28, 2015 at 13:45
  • 1
    msdn.microsoft.com/en-us/library/2yd9wwz4.aspx Commented Feb 28, 2015 at 13:47
  • 1
    msdn.microsoft.com/en-us/library/2s05feca.aspx Commented Feb 28, 2015 at 13:47

2 Answers 2

2

Although your why question is probably unanswerable at this point (it would take one of the original creators of Java to answer it), you may note that a strong design principle of the original Java was simplicity. In that spirit all that Java supports is an array, which on its own gives you an array of arrays as just a special case: such an array whose component type is array.

About the only advantage of a true multidimensional array is the way its members are packed together, offering better cache locality. Such concerns were not high on the list of design priorities of original Java, although today they are getting a much greater share of the spotlight.

Refer to this topic for an in-depth review of pros and cons of multidimensional arrays.

Sign up to request clarification or add additional context in comments.

Comments

0

Short answer: because the language was designed this way. But an array of arrays functions as a multidimensional arrays, so this is not really a limit on the language.

Probably the reason for this is that Java borrowed its array syntax from C and C++, and C and C++ multidimensional arrays are also accessed as though they are arrays of arrays. The difference is that in Java an array of arrays is an array of references to arrays (and thus the arrays in the array can have different lengths).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.