I have to build an array and display its last two rows with all its elements. I understand that the most traditional way of doing it is something like this (I clarify that the "rows" variable was previously loaded by keyboard when asking how many rows and columns the matrix would have):
public void mostrarDosUltimasF() {
System.out.println("fila ante ultima.................");
for (int x = 0; x < mat[filas -1].length; x++) {
System.out.println(mat[filas - 2][x]);
}
System.out.println("Fila ultima............");
for (int x = 0; x < mat[filas-1].length; x++) {
System.out.println(mat[filas - 1][x]);
}
the doubt that I have and that I do not understand, is why I must put the -1 in both for. In the tutorial it says that since the space [0][0] always exists, without that -1 the "out of bounds" error would occur. It's what happened to me when I tried to do it on my own. But I still don't quite understand the reason since if the value of "rows" were, for example, 3, starting the x of the for at 0, three reproductions would suffice. While my logic told me that by putting the -1, the for would only loop twice since the value would be 2. Everything works fine now but there is something I am misunderstanding. Thank you very much.
Imagine you have this array:
If we put positions on it, it would be as follows:
where
filas
is equal to3
, youfor
have the followingwhich would be equal to:
Now if we go back to the matrix we have that
mat[3 - 1]
omat[2]
is the last row:That in your matrix with data would be:
and if we continue replacing you would have the following:
What in the end would be:
Which would help you loop through the three values you have in that row, now if you had:
When performing the
length - 1
del you would havefor
:and so it ensures that whatever the size of the row is, that code will always run for you.
I hope my explanation is understood;) Greetings
One-dimensional arrays start counting from 0, that is, if we have the following code:
If you notice, I am creating an array of 5 elements, however, in order to access each data in the array, you start from position
0
. Graphically this would look like this:So the for loop above will still do 5 iterations and thus print the array data without any problem. But this is because the array positions starts from
0
and ends at positionN-1
(whereN
is the size of array elements).Now, understanding this, we can quickly answer your question:
Let's assume we have a matrix of
5x6
(5
rows and6
columns). So this code should output a6
(because they are6
columns):However, the main reason why you need to subtract from
-1
, is because the positions in each row start counting from0
and end atN-1
.In our example it is valid to do this:
The results that we store in each variable, is the size of the columns of the matrix, but in order to achieve this, we must start with the index
0
and end at the4
.If we did this:
It would give us the following exception: IndexOutOfBoundsException , but this happened, because the index
5
is not valid, it is not something we should do, because the maximum index must be 4 (in this case of course), otherwise we are trying to overflow the buffer and obviously the Java virtual machine will never let this happen and for that reason your program would stop working.Now, then you say the following:
Let us assume that the matrix
3x3
was constructed in this way:Let's assume that p2 represents the indices of the columns and p1 the index of each row.
At first glance, if we see the following code:
It would give us a result
3
. The variablex
must iterate the position0
,1
,2
to display the elements of the array, but notice, I have done 3 iterations, that is, I already went through 3 columns of X row starting from 0 and ending atN-1
.It can't return a
2
, because the column size will be a3
in this case.And even, you could also do it this way (it will still do 3 iterations):
And so, you don't need to subtract
-1
from it, this is done because the indices can't be exceeded2
(in this case it's2
because the maximum column size is3
).