6

I have quite a basic question.

For example:

int a[][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}} 

we say it is [3][4] as it has 3 elements and each element has its own 4 elements.

but how about:

int a[][] = {{1},{5,6,7},{9,10,11,12}} 

is it still [3][4]?


update

so based on the helpful comments below, my first example can be written as a [3][4] array, but the second one cannot be indicated like that, right?

2
  • in both cases you have array of arrays, just in first example all lengths are same Commented Aug 26, 2013 at 19:05
  • 1
    In regards to your update, that is correct. Commented Aug 26, 2013 at 19:16

6 Answers 6

6

These are called Jagged Arrays, and they are not considered to be m-by-n, because they do not fit the formal definition of a Matrix.

In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns.

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

Comments

3

According to java

A multidimensional array is an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length.

Take int a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}}

Here you can't say [3][4] array,but can say it has 3 elements and each element has its own 4 elements.

Take int a[][]={{1},{5,6,7},{9,10,11,12}}
So here you can say array has 3 elements and each element has different number of elements.

2 Comments

so only the array of array has same length of column can be called [m][n], yes?
@WangPeiTheDancer yes they only follow the definition of matrix
2

I just did this test on an Android environment, which will probably work for this case scenario (I think)

int a[][] = { { 1 }, { 5, 6, 7 }, { 9, 10, 11, 12 } };
    for (int i = 0; i < a.length; i++) {
        Log.i("", "i: " + i);
        for (int j = 0; j < a[i].length; j++) {
            Log.i("", "j: " + j);
        }
}

What I got was:

i: 0
j: 0
i: 1
j: 0
j: 1
j: 2
i: 2
j: 0
j: 1
j: 2
j: 3

So, a is not int[3][4].

2 Comments

so only the array of array has same length of column can be called [m][n], yes?
[m][n] refer to the number of columns and row. Each intersection of [m] and [n] must be filled with a value in order to be considered a matrix. Else wise its a jagged array.
0

It's not [3][4].

Its array of array where external array is of dimension "3" and array elements, which are again array, of varying length i.e. 1, 3 and 4 respectively.

Comments

0

No, it is not. When you go like int[][] test = new int[3][4] that's just syntactic sugar. There's nothing magical about a double array.

Both examples are arrays of arrays. Your first happens to have all of those arrays the same legnth ,your second does not.

As a side comment, the vast majority of java programmers use int[][] a notation, rather than int a[][] notation.

Comments

0

There are no 2-dimensional arrays in Java.

There only exist arrays of arrays, where each array can have a different length.

For matrixes, consider a linear memory layout, and using one of the existing libraries.

Note that the following is valid in Java:

int[][] example = new int[2][2];
example[0] = new int[]{1,2,3,4,5,6,7,8,9,10};

while a proper matrix module will not allow this to happen.

This is both a strength and a weakness. There are applications where you just need arrays-of-arrays. And there are cases where it is more efficient (e.g. storing half a triangular matrix only).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.