将带括号的逗号分隔字符串传递给列表

该方法必须接收Stringin 格式"[1,2,3,4,5]",其中仅包含整数,但它也可以包含"[1,2,3,[]]"or "[1,2,3,[1,2,3]]"。我已经有了将数字添加到列表中的代码,但我不知道当我List里面有另一个数字时如何处理。

我想过有一个类List,当我在里面找到另一个括号再次调用该函数时,但当我看到另一个左括号时不知道如何正确地进行子字符串化[。删除括号后,我将字符串分割如下:

String[] arr = string.split(",");

并开始将这些结果添加到List我必须返回 a for loop,我的问题是当我再次看到左括号时,[我必须String再次使用结果子串并调用我的方法,但不知道如何确定右括号。我尝试过的是获取右括号的索引indexOf

String aux = arr[i];
int closingBracket = aux.indexOf("]");

[1,2,3,4,[,6]但如果我有一个不应该被接受的输入字符串,它会接受它。


DIEA
浏览 92回答 1
1回答

至尊宝的传说

您需要做的是在纸上弄清楚什么是有限状态机 (FSM)。当你遇到一个标记(逗号、括号、数字)时,你就进入了一个特定的状态。一旦处于某种状态,您只能接受某些其他令牌,这可能会使您处于不同的状态或使您处于当前状态。您可以有许多不同的状态,它们可以接受不同的令牌并执行不同的操作。例如。当您看到一个数字时,下一个标记可能是。&nbsp;1.&nbsp; Another digit&nbsp;2.&nbsp; A closing bracket.&nbsp;3.&nbsp; A comma.这些令牌中的每一个都可以提示不同的状态和/或动作。&nbsp;1. Another digit - keep on processing since numbers have many digits.&nbsp;2. closing bracket - save the number in a list and get the next list (See below)&nbsp;3. comma - save the number and look for next number假设您想将所有这些保存在列表列表中,最简单的方法是从 a 开始,List<Object>因为您可以将integers和保存lists of lists在具有无限深度的那种类型的列表中。我还建议你看看Stack class。随着解析深度的变化,您可能希望推送当前列表并弹出最近的列表。最后,确保您的括号与以下内容正确匹配。当您看到“[”时增加计数器,当您看到“]”时减少计数器。如果计数器变为负数,则说明右括号过多。如果最后它是正数,则说明您没有足够的右括号。有关这方面的更多信息,请查看维基百科上的有限状态机。我决定用一个小例子来说明我正在谈论的内容。它可能无法涵盖所有可能的边缘情况,但其目的是说明性的。&nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; String strlist =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "[1,2,113],&nbsp; &nbsp; &nbsp;[4,5,[5,2,10],1], [],[1,2,3,4,5,[10,11,12,13,[14,15]]]";&nbsp; &nbsp; &nbsp; int bracketCount = 0;&nbsp; &nbsp; &nbsp; int num = 0;&nbsp; &nbsp; &nbsp; // inital list&nbsp; &nbsp; &nbsp; List<Object> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; // hold sublists for precessing&nbsp; &nbsp; &nbsp; Stack<List<Object>> stack = new Stack<>();&nbsp; &nbsp; &nbsp; char[] chars = strlist.toCharArray();&nbsp; &nbsp; &nbsp; for (int i = 0; i < chars.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char c = chars[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;switch (c) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // state1 left bracket - push current list on stack for later&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // retrieval&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // allocate new list and add it to one just pushed (remember,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // I still have its reference).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Assign nlist to list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // increment bracket count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '[':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stack.push(list);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List<Object> nlist = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.add(nlist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list = nlist;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bracketCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // state2 right bracket - Finished processing current sublist.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if previous tokens were not brackets, then add the&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // number to the list and pop off the previous one.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // decrement bracket count&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ']':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (chars[i - 1] != '[' && chars[i - 1] != ']') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list = stack.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bracketCount--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // state3 - whitespace - ignore&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ' ':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '\t':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // state4 comma - if previous token was not a right bracket,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // then add number. Reset num for next number.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ',':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (chars[i - 1] != ']') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // state5 digit - assumed to be a digit. Each time a digit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // is encountered, update the number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;num = num * 10 + c - '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (bracketCount < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("too many ] brackets at location " + i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (bracketCount > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("insufficent number of ] brackets");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; System.out.println(list);&nbsp; &nbsp;}}请注意,在上面的示例中,“状态”实际上是“伪状态”。也就是说,您不需要检查当前所处的状态来确定如何处理当前令牌或标记错误。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java