Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Two Dimensional Array Definition

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?

like image 400
Wang Pei The Dancer Avatar asked Aug 26 '13 19:08

Wang Pei The Dancer


People also ask

How is a two-dimensional array defined?

We can declare a two-dimensional integer array say 'x' of size 10,20 as: int x[10][20]; Elements in two-dimensional arrays are commonly referred to by x[i][j] where i is the row number and 'j' is the column number.

How do you declare a two-dimensional array in Java?

Here is how we can initialize a 2-dimensional array in Java. int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; As we can see, each element of the multidimensional array is an array itself. And also, unlike C/C++, each row of the multidimensional array in Java can be of different lengths.

What is 1D and 2D array in Java?

Arrays can be created in 1D or 2D. 1D arrays are just one row of values, while 2D arrays contain a grid of values that has several rows/columns. 1D: 2D: An ArrayList is just like a 1D Array except it's length is unbounded and you can add as many elements as you need.


1 Answers

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.

like image 185
Luke Willis Avatar answered Sep 30 '22 12:09

Luke Willis