Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of dimensions when creating multidimensional array

new T[7] creates an array of 7 Ts. If we replace T with int[5], we get new int[5][7] which should create an array of 7 arrays of 5 integers. However, it creates an array of 5 arrays of 7 integers instead. Is there any good reason for this? Wouldn't it make more sense if it was the other way around?

like image 441
fredoverflow Avatar asked Aug 16 '12 19:08

fredoverflow


People also ask

Which one is the correct way to declare multidimensional array?

Which of the following is the correct way to declare a multidimensional array in Java? Explanation: The syntax to declare multidimensional array in java is either int[][] arr; or int arr[][]; 5.

What are the rules to declare multidimensional array?

You must create each multidimensional ARRAY/VARRAY type you need for your applications using a CREATE TYPE (ARRAY form) request. When you create a new multidimensional ARRAY/VARRAY type, you must explicitly specify the lower and upper boundaries for each dimension, as well as the element data type of the array.

Which is the correct way to declare a multidimensional array in C?

Data in multidimensional arrays are stored in row-major order. The general form of declaring N-dimensional arrays is: data_type array_name[size1][size2]....[sizeN];

How are multidimensional array created?

You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.


1 Answers

I think of it as dimensions:

   height width depth
int[5]    [7]   [8]

Or

   rows  cols
int[5]   [7]

That's why it makes sense for it to be 5 arrays of 7 ints to me. There is a kind of natural order.

I think your example also makes sense and is very logical. So I guess it's just a matter of opinion :P

like image 80
Samuel Avatar answered Sep 16 '22 16:09

Samuel