从循环函数返回多个值
原标题:Returning multiple values from a recursive function
I have this problem where I have to convert a decimal number to binary and then store the bits in a linked list where the head node is the most significant bit and the last node is the least significant bit. Solving the problem itself is actually easy as you only need to keep taking the modulo of 2 recursively and add the result in the list until the decimal number becomes 0.
Where I m stuck is that I have to write the function such that it returns a pair of number, (whether an array or a list) of the most significant bit and the last significant bit. i.e: Inputting 14 in the function would return (1, 0), since 14 is 1110 in binary.
I do have access to the MSB and LSB easily(getFirst(), getLast()).
The function can only take one argument which is the decimal number.
Currently I have this current code:
public static void encodeBin(int n) {
if(n == 0) return; //Base case
else {
if(n % 2 == 0)
theList.addFirst(0);
else
theList.addFirst(1);
encodeBin(n / 2);
}
// return?
}
The problem is I can t figure out how return the 2 values. Haveing a return value means I can t call encodeBin() by itself.
Moreover, where should I create the list? If I put something like List = new LinkedList() at the very beginning of the function, then each time the function calls itself, it creates a new list and adds the bits in THAT new list not the original right?(The list created from when the function is called the first time)
Anybody knows how to solve this?
问题回答
You cannot return 2 values. You are going to have to return some object that contains the 2 values. either an array or some new object, depending on your homework requirments and where this function is going to be used.
For the linkedlist creation, what you need is a recursive helper method. Your public method will be used to initialize your objects, start the recursion, and return your result. This allows your actual recursive function to have more than 1 argument.
public static SOME_TYPE encodeBin(int n) {
LinkedList result = new LinkedList();
encodeBin_helper(result,n);
// return the MSB and LSB
}
public static void encodeBin_helper(LinkedList theList, int n) {
if(n == 0) return; //Base case
else {
if(n % 2 == 0)
theList.addFirst(0);
else
theList.addFirst(1);
encodeBin_helper(theList, n/2);
}
}
You can t return two values separately. You can, however, return an array containing the first bit and the last bit or create your own class to hold this data, and return an instance of that class.
And about the list, I see two options:
Make it a static class variable
Make it an argument of the function (although I see you said you couldn t do this).
The first method would look like this:
public class MyClass {
private static List theList = new LinkedList();
// `encodeBin` method as you have it
}
The second method would look like this:
public static void encodeBin(int n, List theList) {
if(n == 0) return; //Base case
else {
if(n % 2 == 0)
theList.addFirst(0);
else
theList.addFirst(1);
encodeBin(n / 2, theList);
}
}
You could then do something along the lines of
List theList = new LinkedList();
encodeBin(14, theList);
and theList would hold the appropriate bits as desired.
As a note, you might want to consider making this a list of booleans instead of integers, with true representing 1 and false representing 0.
I suggest declaring two methods:
(1) public static int[] encodeBin(int n) and (2) private static void encodeBin(LinkedList, int n)
The public method merely creates an empty list and then calls the private version passing both the empty list and the orignal input n as the parameters
something like this:
public static int[] encodeBin(int n) {
LinkedList aList = new LinkedList();
encodeBin(aList , n);
int MSB = aList.getFirst();
int LSB = aList.getLast();
return new int[] {MSB, LSB};
}
private static void encodeBin(LinkedList list, n) {
//your recursive version here
}
相关问题
Spring Properties File
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
What do you say of chopping type-4 UUID in this manner
Check this,
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
combining decorator and state pattern in java - question about OO design
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
Unable to execute stored Procedure using Java and JDBC on SQL server
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
Logging a global ID in multiple components
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications.
[App A] -> [App B] -> [App C]
We set a ...
Java Library Size
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is:
How will the larger, mostly unused ...
How to get the Array Class for a given Class in Java?
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this:
Class arrayOfFooClass = java.lang....
SQLite , Derby vs file system
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database.
How hard is it to migrate ...