in programming there is often such tasks walk through in multi dimensional array (for simplify let take two dimensional) i need indexes of elements in two dimensional array like left to right bottom to up and vice versa and so on please give me a few examples
I would strongly suggest to first go through tutorials available on the internet and then ask questions here. Your problem is not with matrices, but with the language itself, as it seems that you do not understand how fundamental things in programming work.
Also, tag your answers better. Specify the language you are using, etc.
The length of an array arr
, in Java, is always arr.length
. In Java, multidimensional arrays are nothing but array of arrays, so you can successively take the .length
of any "sub-arrays".
int[][] m = {
{ 1, 2, 3, },
{ 4, 5, },
{ 6, 7, 8, 9, 10 },
};
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
System.out.printf("m[%d][%d] = %d%n", i, j, m[i][j]);
}
}
The above prints:
m[0][0] = 1
m[0][1] = 2
m[0][2] = 3
m[1][0] = 4
m[1][1] = 5
m[2][0] = 6
m[2][1] = 7
m[2][2] = 8
m[2][3] = 9
m[2][4] = 10
To represent matrices, it's best to attach to the data structure (arrays or not) two indices M
and N
, which is the dimension of the matrix, and iterate on indices in terms of M
and N
instead of array .length
. This higher abstraction would lead to better readability.