Dimensions for two-dimensional array creation in Java seem backwards

two dimensional array java for loop
write a program to reverse the elements of a given 2d array in java
two dimensional array program in java
how to print two dimensional array in java using for-each loop
how to assign values to two dimensional array in java
two dimensional string array in java example
reverse matrix in java
two dimensional array in java using command line arguments

As far as I understand a two-dimensional array in java is an array of arrays. Analogously to the usual array notation I would expect this to mean that

int[][]

is actually

(int[])[]

(An array of int arrays). Thus I would expect the length of the array

int[][] array = new int[2][3]; // (int[2])[3] 

to be 3 (as it would be an array of length 3 of arrays of length 2). It does however appear to be the other way round.

I tried to find an explanation for this, but was unable to. I found many posts explaining how to initialize multidimensional arrays and that they are arrays of arrays but I was unable to find any explanation of why the order of the indices works out this way.

My only theory is that people find it more natural to iterate over the left index first and Java is built in a way such that this operation is more efficient so the developers made this choice very deliberately.

I am thankful for any source confirming this interpretation or any other reasons why it works out this way.


edit: Since there seems to be some confusion as to why anyone would ever believe using the indices the other way round would be a good idea, here is some clarification:

For a type T the expression new T[5] usually yields an array of size 5. The type int[] seems to be a perfectly good type, however Java does not allow me to say new (int[])[5] and instead forces me to initiate this array of arrays using new int[5][]. I was looking for a reason why Java would do that to me (and got one).


One other thing I would like to add with respect to the (very good) answers for anyone thinking like me: In a world where nobody had ever thought about two-dimensional arrays and you were allowed to create arrays of type int[] as you would create any other array, the following might be perfectly legal code.

    int[][] = new (int[])[5];
    int[] firstArray = new int[3];
    int[0] = firstArray;
    int[0][2] = 1; // last element of first array
    int[0][4] = 2; // IndexOutOfBoundsException, trying to access 5th element of firstArray

This would of course be utterly confusing so I am glad that multidimensional arrays get a special treatment.

An expression like new int[2][5] will create an outer array of length 2, with each element referencing an array of length 5, so a total of 3 arrays are created. This is well-documented in the Java Language Specification, Example 15.10.2-2. Multi-Dimensional Array Creation:

The declaration:

float[][] matrix = new float[3][3];

is equivalent in behavior to:

float[][] matrix = new float[3][];
for (int d = 0; d < matrix.length; d++)
    matrix[d] = new float[3];

It does not mean (int[2])[5], i.e. an array of 5 referencing arrays of 2.

Because of that, the expression to lookup values is evaluated left-to-right, e.g. x[2][5] means (x[2])[5], i.e. in int[][] x variable, lookup x[2], which is an int[], and in that array lookup value at index 5.

int[][] x = new int[9][9];
// z = x[2][5]
int[] y = x[2];
int z = y[5];

How to declare and Initialize two dimensional Array in Java with , How do you determine the size of a 2d array? Hi learners, in this Java tutorial you will learn how to reverse a two-dimensional array in Java. I will show you how easily you can reverse two dimensional array in Java with an easy example. If you don’t know what is a 2d array and learn how to create a 2d array in Java please read this: How to create a dynamic 2D array in Java

The reason for this is the importance of array accesses. You have an array of arrays (int[][]). You then retrieve an array from it (int[]). You then retrieve an element from that (int).

There are two ways of doing this. The first is most important:

for (int i = 0; i < matrix.length; i++) {
    int[] row = matrix[i];
    for int j = 0; j < row.length; j++) {
        int element = row[j];
    }
}

It would be absurd to have to use int[] row = matrix[][i]; in the first example, since the convention is to have array[i] represent the first element of array. In this case, the first element of array is a row, not an element.

The same index ordering is applied consistently throughout.


This is also consistent with the fact that you can have a matrix-like object of strange dimensions with an Object[] array. Perhaps one element of that array will be an int[][][] while the next will be an int[]. It would be a bit odd to have to somehow figure out whether to put array[][][][i] or array[][i] when you don't know the dimensionality of subarray i and the subarrays don't even necessarily have a consistent number of dimensions.

Can you change size of Array in Java once created?, How do you read a two dimensional array in Java? Java, however, lets you create two-dimensional arrays in which the length of each element of the main array is different. Sometimes, this is called a jagged array because the array doesn’t form a nice rectangle. Instead, its edges are jagged. Arrays with more than two dimensions. Java doesn’t limit you to two-dimensional arrays.

You may think of a two dimensional array in Java as an array of arrays. Consider the following general 2D array of numbers:

1 2 3
4 5 6
7 8 9

We could define this array as:

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

Or, we could use an empty constructor, and then populate the individual values:

int[][] array = new int[3][3];
array[0][0] = 1;
array[0][1] = 2;
// etc.

But to better understand how 2D arrays in Java work, consider the following approach:

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

Hopefully it is clear what a 2D array actually is; it is an array of arrays, so we may visualize this as:

[ ] -> [1, 2, 3]
[ ] -> [4, 5, 6]
[ ] -> [7, 8, 9]

That is, each element in the first index of the array, of size 3, corresponding to the rows, points to another 1D array, corresponding to the columns. Note that in Java a 2D array can be jagged, meaning that we could also have done this:

int[][] array = new int[3][];
arr[0] = new int[] {1, 2, 3, 4, 5};
arr[1] = new int[] {1, 2, 3};
arr[2] = new int[] {7, 7, 7};

To address the concern raised by @Henry in a comment below, that the size of the leftmost portion of the above 2D array is 3 agrees with what we see when we draw it out. Namely, we see a 1D array with 3 buckets, each of which points to another 1D array of some size.

Java tutorial with an easy example to show you how to reverse a 2d array in Java​. I will show you how easily you can reverse two dimensional array in Java with an Let's take a look public static void reverse_it(int[][] my_array){; int my_rows = my_array.length; Here I have created this method to reverse the array: by suresh. The Two Dimensional Array in Java programming language is nothing but an Array of Arrays. In Java Two Dimensional Array, data stored in row and columns, and we can access the record using both the row index and column index (like an Excel File). If the data is linear, we can use the One Dimensional Array.

Declaring a two-dimensional array is very interesting in Java as Java 1st Example - Declare the 2D array with both dimensions declaration number is int variable, a is one-dimensional int array and b is a two-dimensional int array. a look at Java Fundamentals Part 1 and Part 2 in Pluralsight, two of the  A two-dimensional entity, in general, is something that has two specific parameters. Those two parameters are usually length and breadth since it is a physical quantity. Similarly, a two-dimensional array is an array which technically has one row of elements, however, each row has a bunch of elements defined by itself.

In this example, we have first created a 2 dimensional array of size 4x4, which means 4 rows and 4 columns. Then we have looped over it two times, first time to​  Two Dimensional Array Program. Two dimensional array can be made in Java Programming language by using the two loops, the first one is outer loop and the second one is inner loop. Outer loop is responsible for rows and the inner loop is responsible for columns. And both rows and columns combine to make two-dimensional (2D) Arrays.

The following article 2D Arrays in Java provides an outline for the creation of 2D arrays in java. We will create only one variable of type array and we will give a size of 100. Before that let us look we have two index values for 2d array. substring() Function in Java · Strictfp in Java · String Reverse Function in Java · Java  Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.

Comments
  • @TimBiegeleisen: Could you explain why you elected to reopen this question? The duplicate answers the specific question as to how the lengths of n-dimensional arrays are calculated.
  • @Makoto The OP isn't just asking how to calculate the length of an array. There is a misconception about how arrays work in Java, which my answer attempts to clarify. If you can find an exact duplicate, then drop a comment and maybe someone else can reclose.
  • why you expected that to be length of 3?
  • @TimBiegeleisen: In that explanation is precisely the same answer that you're providing. It takes more reading, but it isn't the case that the duplicate is inappropriate.
  • I didn't bother to check given how you phrased your comment. I assume you hadn't read this entire question; perhaps I'm in the wrong for not reading your entire duplicate.
  • with each element referencing an array of length 5 ... but jagged arrays are possible as well :-)
  • The last paragraph is not quite supporting your main point (which is excellent). With a multi-dimensional Object array you couldn't put array[i][j] without a cast, since array[i] is nominally of type Object. So even the actual semantics of Java doesn't help here. I think the answer would be stronger without that paragraph. It is worth noting that, say, an int[][] array doesn't need to be rectangular; the first-level elements can be arrays of all different lengths.
  • The idea in the question is that the order of indexing would be reversed, and the outer array would be the last index (e.g., array[][i]). Regardless of casting, this cannot be done coherently with an incoherent number of dimensions to the array. This goes much further than a nonrectangular array.
  • I agree. That is the main point of the first part of your answer. But talking about storing mixed-dimensionality arrays inside an Object[] doesn't seem at all relevant to OP's question. The order of indexing has nothing to do with how difficult it would be to cast an element of an Object[] to the correct array type.
  • All that is true, but does not answer the question why the first index corresponds to the outer array. As the OP says the syntax suggests it would be the inner array.
  • @Henry I disagree that the syntax suggests this. But I do find it confusing, which is why I posted.
  • Even though it does not directly answer my question, the case where just one dimension is defined is actually a better example than my initial one as to why the order would be confusing so thank you for it!