Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

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

```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;
}
}
}```

2. ## 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.