私は再帰のアイデアにかなり慣れていないので、これは実際に再帰メソッドを書く最初の試みです。
最大の要素を出力するために、配列のサイズを保持する変数とともに、配列を渡す再帰関数 Max を実装しようとしました。
機能しますが、気分が良くありません。
また、一般的にクラスメートよりも static 修飾子を使用しているように見えることにも気付きました...
コードを改善する方法について、一般的なヒントとフィードバックを提供してもらえますか?
public class RecursiveTry{
static int[] n = new int[] {1,2,4,3,3,32,100};
static int current = 0;
static int maxValue = 0;
static int SIZE = n.length;
public static void main(String[] args){
System.out.println(Max(n, SIZE));
}
public static int Max(int[] n, int SIZE) {
if(current <= SIZE - 1){
if (maxValue <= n[current]) {
maxValue = n[current];
current++;
Max(n, SIZE);
}
else {
current++;
Max(n, SIZE);
}
}
return maxValue;
}
}