Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

Code Java:

import java.util.*;
public class MaxElement {
public static void main(String[] args) {
// creating array lenght of 5 integers, and receving input from keyboard
int[] a = new int[5];
Scanner tastatura = new Scanner(System.in);
System.out.println("Type elements of array ... ");
for (int i = 0; i < a.length; i++) {
System.out.print("Type " + (i+1) + ". element: ");
a[i] = tastatura.nextInt();
}
//printing max element of array
System.out.print("Max element of array is: ");
System.out.println(a[max(a)]);
}
public static int max(int[] a) {
int n = a.length;
if (n == 1)
return 0;
else {
int i = max(Arrays.copyOf(a,n-1));//this is a problem... i dont know what is returned to int i?
if (a[i] < a[n-1])
return n-1;
else
return i;
}
}
}

Re: Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

Welcome to the Forum! Please read this Announcements - What's Wrong With My Code? to learn how to post code correctly and other useful tips for newcomers.

Please edit your code with code tags

Re: Help me to understand how does this recursive function (max) work int i = max(Arrays.copyOf(a,n-1));

Thanks for fixing your code. A good start to understanding would be to comment the code with your best guess at what it's doing. Once you've done that, post the commented code and let us know where you've gotten stuck.