Java Two Dimensional Array Definition
Asked Answered
E

6

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?

Energetics answered 26/8, 2013 at 19:1 Comment(2)
in both cases you have array of arrays, just in first example all lengths are sameFormication
In regards to your update, that is correct.Prognathous
P
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.

Prognathous answered 26/8, 2013 at 19:8 Comment(0)
S
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.

Setback answered 26/8, 2013 at 19:12 Comment(2)
so only the array of array has same length of column can be called [m][n], yes?Energetics
@WangPeiTheDancer yes they only follow the definition of matrixSetback
N
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].

Nihility answered 26/8, 2013 at 19:10 Comment(2)
so only the array of array has same length of column can be called [m][n], yes?Energetics
[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.Shinar
L
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.

Laise answered 26/8, 2013 at 19:5 Comment(0)
T
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.

Traps answered 26/8, 2013 at 19:5 Comment(0)
C
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).

Crowbar answered 26/8, 2013 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.