Hallo Java-Forum!
So ich brauch mal wieder eure Hilfe. Ich soll den ersten Teil meines Programmes in eine Rekursion umwandeln leider verstehe ich diese noch nicht richtig. Das Programm soll die Anzahl der Werte in a[] ausgeben die grösser als i sind.
So ich brauch mal wieder eure Hilfe. Ich soll den ersten Teil meines Programmes in eine Rekursion umwandeln leider verstehe ich diese noch nicht richtig. Das Programm soll die Anzahl der Werte in a[] ausgeben die grösser als i sind.
Code:
public class IterarionRekusiv {
public static void main(String[] args) {
int[]a={0,1,2,3,4,5,6,7,8,9};
int[]b=new int[a.length];
int i = 2; // veränderbare Variable
System.out.println(doSomething(a,b,i));
System.out.println(doSomethingRek(a,b,0,0,i));
}// end main
static int doSomething (int[]a,int []b,int x) {
int cnt = 0;
for (int i=0; i<a. length ; i++) {
if (a[i] > x) {
b[cnt] = a[i];
cnt ++;
} // end if
}// end for
return cnt ;
}// end doSomeThing
static int doSomethingRek(int[]a,int []b,int x,int cnt,int i) {
if(i==a.length){
return cnt;
}else{
b[cnt]=a[i];
return doSomethingRek(a, b, ++cnt,++i, x);
}
} // end doSomethingRek
}// end InerationRekusiv