gpt4 book ai didi

java - 使用Java(GWT)将这样的String解析为对象结构

转载 作者:行者123 更新时间:2023-11-29 06:02:51 24 4
gpt4 key购买 nike

我正在尝试将 GWT 中的字符串解析为对象结构。不幸的是我做不到。

示例字符串:

"(node1(node2(node3) (node4)) (node5))"
  • "node1" 有 2 个 child :"node2""node5"
  • "node2" 有 2 个 child :"node3""node4"

对象可以是带有 child 的“节点”。对此的任何帮助将不胜感激。

最佳答案

我可以为您提供一个伪代码。我相信以下算法会起作用,但如果您发现它有任何问题,请告诉我。

Create a root node called nodeRoot.
current_node = nodeRoot
For each char in the string:
if the char is '('
add a child node to current_node
parse the string (after the '(' char) to fetch the name of the node
current_node = the newly added child node
else if the char is ')'
current_node = parent of current_node

要跟踪 parent ,您可以使用堆栈。

关于java - 使用Java(GWT)将这样的String解析为对象结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com