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

• March 6th, 2014, 02:07 AM
impaler
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; } } }```
• March 6th, 2014, 02:28 AM
dicdic
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.