I've been working on a problem dealing with recursion, and I need to count the number of calls to the recursive method. I'm stumped. If I have a variable assigned in the method, everytime it enters the method it will get reassigned so I lose my track count. Since I am returning an integer from the method already, I can't return a count. The only thing I can think of is passing another arguement in my method header that has a static variable count to keep track of the number of calls to the method. Does this sound about right? Regards, Michael
You could pass an int[] array, of length 1, as an additional argument to the method. The method should increment the 0'th element of the array before doing anything else. That avoids the need for a static variable, which is generally best to avoid if you ever want to do multi-threading.
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
michael bradly
Ranch Hand
Joined: Oct 06, 2000
Posts: 112
posted
0
This is similar to what I was trying. However, where does the counter get initialized with a value? If I try it like you have it, I get: C:\JavaEx\entryTest\entryTest.java:27: unreachable statement System.out.println("The number of rec calls is" + ++recCount); Thx for the quick responses, Michael
Originally posted by Anthony Villanueva: I suppose you can use an instance variable as a counter. Using a static variable may be dangerous, if you use many instances of this class:
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
Originally posted by michael bradly: This is similar to what I was trying. However, where does the counter get initialized with a value?
If a non-final instance variable is not explicitly initialized it will have a default value. In the case of int, the default value is 0. Can you post your code?
michael bradly
Ranch Hand
Joined: Oct 06, 2000
Posts: 112
posted
0
Sure can... here is the code I am working on...
Originally posted by Anthony Villanueva:
If a non-final instance variable is not explicitly initialized it will have a default value. In the case of int, the default value is 0. Can you post your code?
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
You have another line of code in a block after a "return" statement. The code can never be executed.
michael bradly
Ranch Hand
Joined: Oct 06, 2000
Posts: 112
posted
0
Yeah, I kinda rigged the last return because I kept getting an error: C:\JavaEx\entryTest\entryTest.java:8: missing return statement { and the only way to get it to compile and work was to put in the return for a max at the end. I'm assuming there is an easier way? Again, thanks for the help, Michael
Originally posted by Ron Newman: You have another line of code in a block after a "return" statement. The code can never be executed.
Ron Newman
Ranch Hand
Joined: Jun 06, 2002
Posts: 1056
posted
0
Every possible exit from your method has to return an int. No statement can directly follow a return statement.
michael bradly
Ranch Hand
Joined: Oct 06, 2000
Posts: 112
posted
0
Well, when I place it in front of the return statement I get the error that recCount may not have been initialized.
C:\JavaEx\entryTest\entryTest.java:27: variable recCount might not have been initialized System.out.println("The number of rec calls is" + ++recCount); So close yet so far, Michael
Originally posted by Ron Newman: Every possible exit from your method has to return an int. No statement can directly follow a return statement.
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
Local variables must be explicitly initialized.
michael bradly
Ranch Hand
Joined: Oct 06, 2000
Posts: 112
posted
0
Although it is now noon, the thoughts never stop dawning upon me I've deduced the problem I was having by switching all my variables to the class level. Now everything is working as I would like and my knowledge has increased. Thank you Anthony and Ron.. Regards, Michael
Originally posted by Anthony Villanueva: Local variables must be explicitly initialized.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.