private int array[][] = new int[5][5];
private void arrayIteration(){
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array.length; j++){
array[i][j] = 10;
}
}
}
Can I change the iteration method to recursive one that does the same task?
Edit (this is what I’ve tried but it’s just playing with what I want to do):
private void arrayRecursion(){
if(){
array[i][i] = 10; // Base
return;
}
for(int i = 0; i < array.length; i++){
arrayRecursion();
}
}
Here’s one way you could do it. Basically you start in the 0,0 corner and set it to the value. Then you do 0,1 and 1,0. 0,1 will set, and do 1,1 and 1,2. 1,0 will set and do 2,0 and 1,1. Note that 1,1 just got hit twice – that’s fine because it’s overwriting what it did before. When it hits a ‘wall’ it stops (this means if the [3] array was null, [4] wouldn’t get hit) but that isn’t a problem for arrays allocated with new
int[x][y]notation.which prints the expected 25