First, it is important to bear in mind that a two-dimensional array is nothing more than a one-way array that contains a one-dimensional array at each of its positions, i.e., it is an array of arrays.
source: A bit of arrays | Java and Object Orientation
Therefore, to traverse a two-dimensional array a chained loop or recursion is required.
Traversing two-dimensional arrays with chained loop:
When making a simple loop, with each interaction, you will have access to the array saved at the respective position of the iterated array. If we analogise to a matrix, it would be to obtain each of the lines of a matrix for each interaction. Once this is done we need a second loop within the first to traverse the innermost array and thus access the elements.
NOTE: Although a two-dimensional array can store the representation of an array, we CANNOT consider a two-dimensional array as an array, since most languages allow the internal arrays stored by the outermost array to be of different size, which means that in each row can contain a number of different columns and this has to be taken into account when traversing a two-dimensional array.
Example that prints the elements of a bidimencional array in Python:
a = [[1,2],[3,4],[5,6]]
for i in a:
for j in i:
print(j)
Example that prints the elements of a bidimencional array in java using traditional and foreach:
public class ExemploListaBidimencional {
public static void main(String[] args) {
int[][] lista = {{1,2},{3,4},{5,6}};
// Usando for
for (int i = 0; i < lista.length; i++) {
for (int j = 0; j < lista[i].length; j++) {
System.out.println(lista[i][j]);
}
}
// Usando foreach
for (int[] i : lista) {
for (int j : i) {
System.out.println(j);
}
}
}
}
Traversing two-dimensional arrays using recursion:
The concept is the same, but instead of using a throw let’s call a function to treat the first element and call the same function recursively to treat the next element paying attention to the stop condition that in this case is the end of the array.
Example that prints the elements of a bidimencional array recursively in java:
public class ExemploListaBidimencional {
public static void main(String[] args) {
int[][] lista = {{1,2},{3,4},{5,6}};
percorreArrayBidimencional(lista, 0, lista.length);
}
private static void percorreArrayBidimencional(int[][] lista, int inicio, int fim) {
if(inicio < fim) {
percorreArray(lista[inicio], 0, lista[inicio].length);
percorreArrayBidimencional(lista, inicio + 1, fim);
}
}
private static void percorreArray(int[] lista, int inicio, int fim) {
if(inicio < fim) {
System.out.println(lista[inicio]);
percorreArray(lista, inicio + 1, fim);
}
}
}
Traversing the array of your case:
for(Tile[] tilesRows :tileset) {
for(Tile tile : tilesRows){
tile.width = bmp.getWidth() / bmp_columns;
tile.height = bmp.getHeight() / bmp_rows;
}
}
I made an update on the answer according to the information asked in the question. See if it clarifies.
– Augusto Vasques
Thank you! Thank you very much!
– Victor Rafael Pereira Alves