Skip to content
Advertisement

Java multidimensional arrays – How to refer to the length of different dimensions

We are learning about 2D arrays in my AP computer science A class and I have a question that I’m struggling to find the clear answer to. For some context when I’m talking about my question, ragged arrays are out of the picture and when I refer to rows I am talking about arrays going across and when I refer to columns I’m talking about arrays that go up in down in the matrix.

Example of a matrix with 2 rows and 4 columns:

0 0 0 0
0 0 0 0

My question is how would I refer to the column length and the row length. In some online resources I have looked at, it describes the number of rows as arr.length and the number of columns as arr[0].length (I know that could be arr[1].length as well but since I’m not talking about ragged arrays I’m just going to use 0) and in other places it describes the row length as arr[0].length.

On my test I was marked incorrect for saying that the column length is arr.length and the correct answer was arr[0].length. I’ve seen various answers online and in lecture videos I have watched so I wanted to clarify here.

Edit: I’m specifically asking if arr.length refers to the column length; I know that is refers to the number of rows.

Advertisement

Answer

On my test I was marked incorrect for saying that the column length is arr.length and the correct answer was arr[0].length

Well, there is your problem. The Java Language Specification does not mention the notion of ‘row’ or ‘column’. It’d have been mighty strange if it did. It’s a programming language, the whole point of them is that they are flexible constructs and the world is your oyster. Program whatever you like.

Which gets us to this simple truth: The language provides arrays and dimensional syntax (even if java doesn’t really multidimensional arrays, it’s just arrays-of-arrays). You make up everything else, notably including the concept of row and column.

In other words, arr.length is the row length, and arr[0].length is the column length? Okay. Well, you tell me. It’s your program.

Your teacher is really wrong here, capital W: Not just that they marked down “arr.length is the row” (because that is not wrong. It is also not right, it is a fundamental misunderstanding – it’s like saying: “That feeling is tuna” – 2 concepts that are utterly unrelated), but mostly that they are perpetuating a significant brainfart by even suggesting that there is such a thing: By suggesting that it is even meaningful to state, in blanket fashion, that dim1 is rows and dim2 is columns (or vice versa) for all java code out there.

I take it all back, though, if the point of saying ‘Say, Benny, you are wrong – arr.length is not the number of columns’ is to explain to you that it isn’t the number of rows either: It’s whatever you want it to represent. It’s also possible you’ve glossed over other parts, for example, if the question is shaped more like this:

int[][] chessBoard = new int[8][8];
chessBoard[1][0] = new Knight(Color.WHITE);

In this specific example, assuming that it’s fair game to presume knowledge of the rules of chess (dubious but then I’m not responsible for teaching), you may safely conclude that chessBoard.length gives you the number of columns, and chessBoard[0].length the number of rows; after all, the first row, second column does indeed start out containing a knight, whereas the second row, first column certainly does not (it holds a pawn).

But that is only because whoever wrote that snippet of chess software decreed it so. They could have decreed that the first ‘dimension’ is the row and the second is the column just the same. It’d have an obvious effect on just about every other line of code that interacts with the chessBoard array, but the code is no less or more valid and no less or more weird.

TL;DR: Either you misjudged the point of this question or your teacher is didactically speaking completely off.

NB: Note that absolutely nothing in your question indicates on its own which decree has been made: That ‘first dimension’ holds rows and ‘second dimension’ holds columns or vice versa. Your example of:

0 0 0 0 
0 0 0 0

still doesn’t mean anything. That’s like showing me a picture of a chess board, that still doesn’t explain which java array dimension maps to which dimension in your picture. Had the example included java code, different story. Another example of this:

Had the question read instead:

Example of a matrix with 2 rows and 4 columns.

int[][] matrix =
   {0, 0, 0, 0}, {0, 0, 0, 0};

Then the code gives away the answer. Evidently, the first dimension is rows and the second is columns (because matrix.length would return 2 and matrix[0].length would return 4, and the question literally states “This code represents a matrix of 2 rows and 4 columns”. Both pieces (the code and the question preamble) together only make sense if the decree is made that dim1 = rows, dim2 = cols.

For what it is worth, I bet that if you do a deep scan of all java code on all of github, that dim1 = rows and dim2 = cols is significantly more common than the reverse, for all 2D matrix code that clearly represents something that can be conceptualized as ‘a thing with rows and columns’. But I absolutely guarantee you that there is perfectly fine code out there that uses dim1 = cols and dim2 = rows, and if you file a bug report to switch them, you will be correctly. laughed out of their issue tracker.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement