Find the max number in a list using recursion Java -


this question has answer here:

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 assignment asks me find largest number in list numbers using recursion.

the above code throwing index exception believe being caught in main not have access to.

what wrong code / logic?

edit

thanks replies.

i went ahead , removed first counter, understand broke there, still not allow me find max number.

here's assignment:

/*  * findmaxrecursively  *  * takes list of numbers , finds largest among them  * using recursive calls.  *  * @param numbers list of numbers, can odd or numbered  * @return largest number in list  *  * hint: base case may comparison of 2 numbers  */ 

am correctly executing recursion doing :

return finmaxrecursively(numbers): 

if want find max value present in array using recursion use below.

public static int findmax(int[] a, int index){     if (index > 0) {         return math.max(a[index], findmax(a, index-1))     }      else {         return a[0];     } } 

specifically arraylist can use below method

public static final int findmaxrecursively(list<integer> numbers)  {      if(numbers.size() == 1)          return numbers.get(0);       int bottom = findmaxrecursively(numbers.sublist(0,numbers.size()/2));       int top = findmaxrecursively(numbers.sublist(numbers.size()/2,numbers.size()));       return top>bottom?top:bottom; } 

also take find maximum value in array recursion


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -