public static int counter = 0 ;
public static int max = 0 ;
public static final int findMaxRecursively(List<Integer> numbers) {
if (numbers.get(counter) > max){
max = numbers.get(counter);
counter++;
}
if (counter == numbers.size() - 1){
return max;
}
counter++;
return findMaxRecursively(numbers);
}
I have an assignment that asks me to find the largest number in the list numbers using recursion.
The above code is throwing an index exception which I believe is being caught in main which I do not have access to.
What is wrong with my code / logic?
EDIT
Thanks for the replies.
I went ahead and removed the first counter, I understand what I broke there, but that still does not allow me to find the max number.
Here's the assignment:
/*
* findMaxRecursively
*
* Takes a list of numbers and finds the largest among them
* using recursive calls.
*
* @param numbers a list of numbers, can be odd or even numbered
* @return the largest number in the list
*
* Hint: your base case may be a comparison of 2 numbers
*/
Am I correctly executing recursion by doing :
return finMaxRecursively(numbers):