java中递归的替代方法

发布于 2024-09-28 20:42:22 字数 1924 浏览 4 评论 0原文

有没有替代递归的方法?我正在处理的课程如下 TibrvMsg 这是消息类,其中包含 TirvMsgField 类型的字段 TibrvMsg 还可以包含 TibrvMsg 类型的 TirvMsgField。这意味着消息可以包含消息本身的字段。 我可以使用递归来打印所有字段。但我想修改字段并将其添加到另一个消息中。我想知道是否有其他替代递归的方法?

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; 

} 

} getMsg() 方法返回示例消息。在 getModMsg() 中,我使用递归并且它有效,这意味着我能够打印每个字段和子字段。现在在这个方法中,我想修改字段属性并更新消息。Means 方法应该返回修改后的消息。因此我使用:

field.id = id++; msg.updateField(字段); 这不起作用。我现在想要的是使用上述函数创建修改后的消息。

Is there any alternative way to recursion? The classs i am dealing are as follows
TibrvMsg This is message class which contains fields of type TirvMsgField
TibrvMsg can also contain TirvMsgField of type TibrvMsg.That means message can contain fields which are message themselves.
I can use recursion to print all the fields.But i want to modify fields and add the to another Message. I am wondering if there is any alternative way to recursion?

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; 

} 

}
The method getMsg() returns sample message.In getModMsg() i m using recursion and it works, that means i am able to print each and every field and subfield. Now in this method, i want to modify the field properties and update the message.Means method should return the modified message.Hence i m using :

field.id = id++;
msg.updateField(field);
This is not working.What i want now is to create a modified message using above functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

智商已欠费 2024-10-05 20:42:22

有直接递归和间接递归,其中方法调用自身,而间接递归则方法调用另一个方法,而该方法会调用原始方法。实际上没有任何“递归的替代方法”,因为递归只是一个概念。

There is direct recursion where a method calls itself and indirect recursion where a method calls another method that somewhere down the line will call the original method. There isn't really any "alternative way to recursion" as recursion is just a concept.

溺渁∝ 2024-10-05 20:42:22

我仍然不明白为什么你不能使用递归。但这里有一个想法。几乎任何用递归编写的东西都可以使用堆栈和循环来编写。使用循环可能会变得非常混乱,但它会得到非递归。希望这有帮助。

I am still not understanding why you can't use recursion. But here is an idea. Almost anything written in recursion can be written using a stack and a loop. It can become really confusing using a loop but it will get that non-recursion. Hope this helps.

世界等同你 2024-10-05 20:42:22

您始终可以使用循环来执行递归操作(否则不支持递归的语言将无法执行某些操作)。

You can always use loops to do what you would do with recursion (otherwise languages which don't support recursion would not be able to do things certain things).

原谅我要高飞 2024-10-05 20:42:22

您的代码按预期修改了字段 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 ;
}

如果您仍然认为需要避免递归,您可以使用堆栈并推送需要审核的消息。

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 ;
}

希望这对您有所帮助,最重要的是,解决您的问题。

Your code modifies the field id as expected, the problem when you call updateField the action is on the chained message. What you need is to maintain the root or top message reference so you can add the fields to it.

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 ;
}

If you still think you need to avoid recursion you can just use a stack and push the messages that need to be review.

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 ;
}

Hope this helps and ,most important, solve your problem.

没企图 2024-10-05 20:42:22

有点模糊的问题,但我觉得查看 Composite 设计模式可能会帮助你解决问题你需要什么。

Kind of a vague question, but I feel like looking over the Composite design pattern might help you sort out what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文