java中递归的替代方法
有没有其他的递归方法?我正在处理的班级如下 TIBRVMG这是一个消息类,其中包含TIRVMSG字段类型的字段 TIBRVMG还可以包含类型为TIBRVMG的TIRVMSG字段。这意味着消息可以包含消息本身的字段。 我可以使用递归打印所有字段。但我想修改字段并将其添加到另一条消息中。我想知道是否有递归的替代方法java中递归的替代方法,java,recursion,Java,Recursion,有没有其他的递归方法?我正在处理的班级如下 TIBRVMG这是一个消息类,其中包含TIRVMSG字段类型的字段 TIBRVMG还可以包含类型为TIBRVMG的TIRVMSG字段。这意味着消息可以包含消息本身的字段。 我可以使用递归打印所有字段。但我想修改字段并将其添加到另一条消息中。我想知道是否有递归的替代方法 import com.tibco.tibrv.*; public class ShowMsg { static TibrvMsg modMsg =new TibrvMsg();
import com.tibco.tibrv.*;
public class ShowMsg {
static TibrvMsg modMsg =new TibrvMsg();
static int id = 0;
public static void main(String[] args) throws TibrvException{
TibrvMsg msg = getMsg();
TibrvMsg modMsg = getModMsg(msg);
//System.out.println(modMsg);
for(int i=0;i<modMsg.getNumFields();i++){
TibrvMsgField field = modMsg.getFieldByIndex(i);
System.out.println(field.name+"-------"+field.id);
}
}
public static TibrvMsg getMsg(){
TibrvMsg msg = new TibrvMsg();
try{
TibrvMsg subMsg = new TibrvMsg();
subMsg.add("S1","43333");
subMsg.add("S2","7377773");
subMsg.add("S3","8388883");
//subMsg.add("SUBSUB", subSubMsg);
msg.add("Field1", "JJSJJS");
msg.add("Field2", "JDSKJKS");
msg.add("Field3", "9299399");
msg.add("Field4", "HHJJSJJSJ");
msg.add("SUB",subMsg);
}
catch(TibrvException rv){
System.out.println(rv);
}
return msg;
}
public static TibrvMsg getModMsg(TibrvMsg msg){
try{
int total = msg.getNumFields();
for(int i=0;i<total;i++){
TibrvMsgField field = msg.getFieldByIndex(i);
if(field.type==TibrvMsg.MSG){
getModMsg((TibrvMsg)field.data);
}
else{
field.id = id++;
msg.updateField(field);
}
}
}
catch(TibrvException rv){
System.out.println(rv);
}
return msg;
}
导入com.tibco.tibrv.*;
公共类ShowMsg{
静态TIBRVMG modMsg=新TIBRVMG();
静态int id=0;
公共静态void main(字符串[]args)抛出TIBRVEException{
TibrvMsg msg=getMsg();
TibrvMsg modMsg=getModMsg(msg);
//System.out.println(modMsg);
对于(int i=0;i有直接递归,其中一个方法调用自身,间接递归,其中一个方法调用另一个方法,该方法在某个地方将调用原始方法。实际上没有任何“递归的替代方法”因为递归只是一个概念。我仍然不明白为什么不能使用递归。但这里有一个想法。几乎任何用递归编写的东西都可以使用堆栈和循环来编写。使用循环会让人感到困惑,但它会得到非递归。希望这能有所帮助。你可以一直使用循环来完成你想做的事情使用递归(否则不支持递归的语言将无法完成某些事情).这是一个模糊的问题,但我觉得查看设计模式可能会帮助您理清所需内容。您的代码按预期修改字段id,调用updateField时的问题是操作在链接消息上。您需要的是维护根消息或顶部消息引用,以便可以向其中添加字段
private static class Context {
private int sequence ; // to avoid using the static var id (Thread-Safety)
private TibrvMsg head ; // The message we want to receive the childs data
private TibrvMsg current ; // Message to work in the level of recursion
}
public static TibrvMsg getModMsg(TibrvMsg message) {
Context context = new Context() ;
context.sequence = 0;
context.head = message ; // can be a new message ;)
context.current = message ;
return getModMsg(context) ;
}
public static TibrvMsg getModMsg(Context context) {
TibrvMsg current = context.current ;
int total = current.getNumFields() ;
for(int idx = 0; idx < total; idx++)
{
TibrvMsgField field = current.getFieldByIndex(idx);
if (field.type == TibrvMsg.MSG) {
context.current = (TibrvMsg)field.data ;
getModMsg(context) ;
} else {
field.id = context.sequence++;
context.head.updateField(field);
}
}
return current ;
}
私有静态类上下文{
private int sequence;//避免使用静态变量id(线程安全)
private tibrvmg head;//我们想要接收孩子数据的消息
private tibrvmg current;//在递归级别工作的消息
}
公共静态TIBRVMG getModMsg(TIBRVMG消息){
上下文=新上下文();
context.sequence=0;
context.head=message;//可以是新消息;)
context.current=消息;
返回getModMsg(上下文);
}
公共静态TIBRVMG getModMsg(上下文){
TIBRVMG current=context.current;
int total=current.getNumFields();
对于(int idx=0;idx
如果您仍然认为需要避免递归,那么可以使用堆栈并推送需要检查的消息
public static TibrvMsg getModMsg(TibrvMsg message) {
Stack<TibrvMsg> stack = new Stack<TibrvMsg>() ;
stack.push(message);
while (!stack.isEmpty())
{
TibrvMsg current = stack.pop() ;
int total = current.getNumFields() ;
for(int idx = 0; idx < total; idx++)
{
TibrvMsgField field = current.getFieldByIndex(idx);
if (field.type == TibrvMsg.MSG){
stack.push((TibrvMsg)field.data);
} else {
field.id = id++;
message.updateField(field);
}
}
}
return message ;
}
公共静态TIBRVMG getModMsg(TIBRVMG消息){
堆栈=新堆栈();
stack.push(消息);
而(!stack.isEmpty())
{
tibrvmg current=stack.pop();
int total=current.getNumFields();
对于(int idx=0;idx
希望这有助于解决您的问题。很难理解您的问题,也许您可以通过发布课堂上的小摘录来让问题变得更具体问题中。你能重新表述一下这个问题吗?实际上,你问的问题并不清楚。@user419229:我已经添加了递归标记,因为这显然与问题的内容有关(实际上比Java更重要)。正如其他人所说,请重新表述问题,使其更清楚你在问什么。这应该会提高答案的质量。伙计,给我们这两个类的最低定义。为了澄清,当你提到不支持递归的语言时,你的意思是它们没有尾部调用优化。显然,你可以在Java,但深度递归会导致调用堆栈增长。这是JVM的一个限制,我真的希望他们能解决这个限制。如果不是为了Java,也为了其他语言,如Scala和Clojure。@dsmith有一些TC语言不支持递归:-),尽管(特别是现在)dsmith:我想他指的是不支持递归的语言,比如FORTRAN 77。在FORTRAN 77中,函数调用自身是编译器错误,而且由于局部变量通常是静态分配的(与C中的堆栈分配相反)是的,我指的是不支持递归的语言,比如FORTRAN 77。很公平。从80年代末开始,我就没有写过一行FORTRAN,甚至那时我也没有做任何实际工作:-)。以前从未听说过“直接”和“间接”递归。我习惯于“递归”函数和“相互递归”函数。“直接”和“间接”是我学过的术语,也是维基百科使用的术语,众所周知,维基百科是所有事实的无可辩驳的来源:谢谢。这真的解决了问题。