I have a homework assignment that is dealing mainly with vector modification through recursion. I understand recursion, and I read through this thread (http://www.dreamincode.net/forums/showtopic19886.htm) which showed how to create a counter through recursion instead of a for loop. Unfortunately, I can't create a counter by placing it in the signatures of my method, since my wonderful professor has decided that the best way to code is through him setting signatures and not letting us change them. Oh well, I guess I have to deal with it. This method is supposed to take a Vector (here's a link to the javadoc page, http://java.sun.com/...til/Vector.html, for some reason no one I've talked to outside my school has ever heard had to use them before) and initialize each element in it to a specified value. I understand recursion, and know how to actually get each value initialized, its just that I'm having trouble figuring out how to get a counter going so I don't fall into an infinite loop, without setting the counter up in the signature of the method.
This is what I have so far:
/**
* Initialize every element of a Vector to a given value.
*
* @param v A Vector with at least one element.
* @param x The value to initialize each element to.
*
*/
public void initialize(Vector<Integer> v, int x)
{
int i = v.size();
int lastNum = v.get(i-1);
if ((lastNum!=x)&&(i-1>0))
{
v.setElementAt(x,i);
initialize(v,x);
}
else if ((lastNum==x)&&(i-1>0))
{
i=i-1;
v.setElementAt(x, i);
initialize(v,x);
}
if ((i-1>=0))
{
return;
}
} // end of initialize method
EDIT: oops, forgot to put my updated code in, its all better now.
Thanks in advance for any help you can give me!
This post has been edited by lwnexgen: 27 March 2007 - 03:58 PM

New Topic/Question
Reply



MultiQuote


|