猿问

将格式化的字符串解析为数组

我正在使用以这种格式接收字符串的 InputSteam。

[[1,3,4],[43,6,3],[4,2,5]]

我将如何将其解析为可用的 java 数组?


慕妹3242003
浏览 201回答 2
2回答

慕丝7291255

这是在不使用第 3 方库的情况下解析该文本的一种方法。public static int[][] parse(String text) {&nbsp; &nbsp; if (! text.startsWith("[[") || ! text.endsWith("]]"))&nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("Invalid text: " + text);&nbsp; &nbsp; String[] subTexts = text.substring(2, text.length() - 2).split("\\],\\[");&nbsp; &nbsp; int[][] result = new int[subTexts.length][];&nbsp; &nbsp; for (int i = 0; i < subTexts.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; String[] valueTexts = subTexts[i].split(",");&nbsp; &nbsp; &nbsp; &nbsp; result[i] = new int[valueTexts.length];&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < valueTexts.length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i][j] = Integer.parseInt(valueTexts[j]);&nbsp; &nbsp; }&nbsp; &nbsp; return result;}测试System.out.println(Arrays.deepToString(parse("[[1,3,4],[43,6,3],[4,2,5]]")));输出[[1, 3, 4], [43, 6, 3], [4, 2, 5]]

慕尼黑8549860

这是我的尝试(虽然有点冗长)。该程序int[][]在每种情况下都使用以下三个输入字符串创建了- 单独尝试时:"[[1,3,4],[43,6,3],[4,2,5]]""[[]]""[[],[1,2,3],[0]]"程序代码:import java.util.*;import java.util.stream.*;public class ArrayParsing {&nbsp; &nbsp; private final static String input = "[[1,3,4],[43,6,3],[4,2,5]]";&nbsp; &nbsp; private final static String open = "[";&nbsp; &nbsp; private final static String open2 = "[[";&nbsp; &nbsp; private final static String close = "]";&nbsp; &nbsp; private final static String close2 = "]]";&nbsp; &nbsp; private final static List<int []> outerContent = new ArrayList<>();&nbsp; &nbsp; public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp; String content = "";&nbsp; &nbsp; &nbsp; &nbsp; String token = "";&nbsp; &nbsp; &nbsp; &nbsp; boolean flag = false;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < input.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; token = input.substring(i, i+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (token.equals(open) || token.equals(open2)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (token.equals(close) && !flag) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buildArray(content);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content = content.concat(token);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int [][] result = outerContent.toArray(new int[outerContent.size()][]);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.deepToString(result));&nbsp; &nbsp; }&nbsp; &nbsp; private static void buildArray(String in) {&nbsp; &nbsp; &nbsp; &nbsp; int [] intArray = new int[0];&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (in.trim().length() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String [] elements = in.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intArray = Arrays.stream(elements)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.mapToInt(s -> Integer.parseInt(s))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.toArray();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; outerContent.add(intArray);&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答