Welcome to Dream.In.Code
Become a Java Expert!

Join 149,499 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,385 people online right now. Registration is fast and FREE... Join Now!




Vector modification through recursion

 
Reply to this topicStart new topic

Vector modification through recursion, Issues setting up a counter without initializing it in the method sign

lwnexgen
27 Mar, 2007 - 02:08 PM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
Hey Guys-

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/j2se/1.4.2/docs/api/ja...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:

CODE

/**
     * 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 Mar, 2007 - 02:58 PM
User is offlineProfile CardPM
+Quote Post

lwnexgen
RE: Vector Modification Through Recursion
27 Mar, 2007 - 04:47 PM
Post #2

New D.I.C Head
*

Joined: 27 Mar, 2007
Posts: 12


My Contributions
Does anyone have any way they can help me on this? It's really confusing the hell out of me...

All I really need to know is how to work a counter using recursion instead of loops. We receive a 0 on the entire assignment if it has any loops in it at all...
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month