質問するログイン新規登録

回答編集履歴

1

再帰呼出し関数をひとつにするコードを追加

2021/06/03 03:22

投稿

kazuma-s
kazuma-s

スコア8222

answer CHANGED
@@ -20,4 +20,34 @@
20
20
  }
21
21
  }
22
22
  }
23
+ ```
24
+ **追記**
25
+ 再帰呼出しの関数を 1つにすることもできます。
26
+ ```Java
27
+ class Main {
28
+ public static void main(String[] args) {
29
+ int[][] numbers = {{1, 2, 3}, {4, 5}, {6}};
30
+ func(numbers, 0, 0);
31
+ }
32
+
33
+ static void func(int[][] a, int i, int j) {
34
+ if (i >= a.length) return;
35
+ int n = a[i].length;
36
+ if (j < n) {
37
+ System.out.printf(j == n-1 ? "%d%n" : "%d ", a[i][j]);
38
+ func(a, i, j+1);
39
+ }
40
+ else func(a, i+1, 0);
41
+ }
42
+ }
43
+ ```
44
+ でも、やっぱり deepToString の出力を正規表現で編集するのが簡単でしょう。
45
+ ```Java
46
+ class Main {
47
+ public static void main(String[] args) {
48
+ int[][] numbers = {{1, 2, 3}, {4, 5}, {6}};
49
+ System.out.printf(java.util.Arrays.deepToString(numbers)
50
+ .replaceAll("], |]]", "\n").replaceAll("\[|,", ""));
51
+ }
52
+ }
23
53
  ```