fork download
  1. import java.util.Arrays;
  2. public class Main {
  3. public static void main(String[] args) {
  4. System.out.println("Original: " + Arrays.deepToString(twoDimArray2));
  5. replaceAll(2, 99, 0, 0);
  6. System.out.println("Replaced: " + Arrays.deepToString(twoDimArray2));
  7. }
  8.  
  9. private static double[][] twoDimArray2 = new double[][] { { 1, 2, 2 }, { 3, 2, 4 } };
  10.  
  11. private static void replaceAll(double number, double replacementTerm, int i, int j) {
  12. double searchFor = number;
  13. double replace = replacementTerm;
  14. if (twoDimArray2[i][j] == searchFor) {
  15. System.out.println("Replaced An Element!");
  16. twoDimArray2[i][j] = replace;
  17. System.out.println(twoDimArray2[i][j]);
  18. }
  19. if (i == twoDimArray2.length - 1 && j == twoDimArray2[0].length - 1) {
  20. System.out.println("Reached end!");
  21. return;
  22. }
  23. if (i + 1 < twoDimArray2.length) {
  24. replaceAll(number, replacementTerm, i + 1, j);
  25. }
  26. if (j + 1 < twoDimArray2[0].length) {
  27. replaceAll(number, replacementTerm, i, j + 1);
  28. }
  29. }
  30. }
Success #stdin #stdout 0.1s 36172KB
stdin
Standard input is empty
stdout
Original: [[1.0, 2.0, 2.0], [3.0, 2.0, 4.0]]
Replaced An Element!
99.0
Reached end!
Replaced An Element!
99.0
Reached end!
Replaced An Element!
99.0
Reached end!
Replaced: [[1.0, 99.0, 99.0], [3.0, 99.0, 4.0]]