오히려 간단
Arrays.deepToString(array).replace(" ", "");
좀 더 까다 무한 중첩 목록이 열로 변환.
이
/**
* The following code is only for demonstration purposes.
* It does neither do any validation on the input String
* nor work with more than one digit numbers.
*/
static int index = 0; // the position in the String
Object buildList(String nestedList) {
List<Object> list = new ArrayList<>();
while (index < nestedList.length()) {
char c = nestedList.charAt(index++);
if (c == '[') // add a sub-list via a recursive call
list.add(buildList(nestedList));
else if (c == ']') // stop building the list
break;
else if (c == ',') {} // do nothing
else // add an element to the list
list.add(c);
}
return list;
}
예 전화 :
이
System.out.println(buildList("[3,[4,3],5]")); // prints [3, [4, 3], 5]
참고 :이
위의 코드는 (적어도 내가 무엇을 생각 하는가 있지만 가장 쉬운 해결책은 아마도 재귀를 사용하는 것) 달성하기를 원한다면, 실제로는 그러한 데이터 구조를 사용하는 것은 바람직하지 않을 수 있습니다. 왜냐하면 중첩 목록에 대한 액세스가 상당히 복잡하고 일부 캐스트가 포함되기 때문입니다 ing.
더 나은 해결책은 아마도 각 노드가 다른 노드에 대한 링크를 포함하여 값 목록을 가진 일종의 트리 데이터 구조를 사용하는 것입니다. (http://en.wikipedia.org/wiki/Tree_(data_structure))
내 코드가 스택을 사용하고 StackOverflowException이 있으며 StackOverflow.com에 게시 한 점이 무엇입니까? – Kyranstar
여기서 예상되는 결과는 무엇입니까? 내가 아는 경우, 나중에 몇 가지 아이디어를 게시 할 수 있습니다 –
[채팅에서 계속 토론하십시오] (http://chat.stackoverflow.com/rooms/57796/discussion-between-kick-buttowski-and-kyranstar). –