|
|
Re: searching array using recursion
|
Posted: Oct 29, 2004 11:58 AM
|
|
For a recursion you need a stop condition.
To find a stop condition you need an input value.
Using recursion on an array only to find the biggest value sounds kinda sensless to me, but here we go: Just transform the array into a Vector.
Here's your solution:
public int getMaximum () { Vector v = new Vector(); for (int i = 0; i < anArray; i++) v.add(new Integer(anArray[i]); if (v.size() > 0) return getMaximum (v); return -9999999999; } private int getMaximum (Vector v) { int val = (Integer)v.get(0); v.removeElementAt(0); return (v.size() <= 0)? val : Math.max (val, getMaximum (Vector v)); }
I know this looks stupid, but it's one way to do it.
|
|