[英]How to turn a String into a nested Array
I get a String of the format <num-num-num><num-num-num><num-num-num>
.我得到一个格式为<num-num-num><num-num-num><num-num-num>
的字符串。 I want to turn this into a nested Array of Ints with each Array being the content between the <>
.我想把它变成一个嵌套的整数数组,每个数组都是<>
之间的内容。
This is what I got so far:这是我到目前为止得到的:
String parameter = args[1];
// split the string into an array of strings at >
String[] splitString = parameter.split(">");
int[][] square = new int[splitString.length][splitString.length];
// remove <, > and - characters and push the numbers into the square
for (int i = 0; i < splitString.length; i++) {
splitString[i] = splitString[i].replaceAll("[<>-]", "");
for (int j = 0; j < splitString.length; j++) {
square[i][j] = Integer.parseInt(splitString[i].substring(j, j + 1));
}
}
I don't feel like this is very clean, but it works.我觉得这不是很干净,但它确实有效。 Does anyone have an idea on how to improve readability?有没有人知道如何提高可读性?
I assume "<num-num-num><num-num-num><num-num-num>"
would define a 3x3 grid so I would use the following approach:我假设"<num-num-num><num-num-num><num-num-num>"
会定义一个 3x3 网格,所以我会使用以下方法:
"num-num-num"
.将字符串拆分为每行一个字符串,以便我们得到"num-num-num"
。 That means:这意味着:
"><"
and remove it在"><"
处拆分并将其删除"-"
to get the individual numbers在"-"
处拆分每一行以获得单独的数字Some code example:一些代码示例:
String input = "<11-12-13><21-22-23><31-32-33>";
//remove leading < and trailing > then split at ><
String[] inputRows = input.substring(1,input.length()-1).split("><");
int[][] grid = new int[inputRows.length][];
for( int r = 0; r < inputRows.length; r++) {
//split the row at -
String[] cells = inputRows[r].split("-");
//convert the array of strings to an array of int by parsing each cell
grid[r] = Arrays.stream(cells).mapToInt(Integer::parseInt).toArray();
}
Here's how it can be done using Java 9 Matcher.results()
.以下是如何使用 Java 9 Matcher.results()
来完成。
First, let's define a pattern that captures a group of digits interlaid with dashes -
:首先,让我们定义一个模式来捕获一组用破折号-
交织的数字:
public static final Pattern ANGLE_BRACKETS = Pattern.compile("<([\\d-]*)>");
Using this pattern, we can create a matcher, and with Matcher.results()
generate a stream of MatchResult
s, which is an object representing the captured sequence of characters.使用此模式,我们可以创建一个匹配器,并使用Matcher.results()
生成一个 stream 的MatchResult
s,这是一个 object 表示捕获的字符序列。
We can obtain the matching substring using MatchResult.group()
( 1
should be passed as an argument, since we need a substring without brackets ).我们可以使用MatchResult.group()
获得匹配的 substring( 1
应该作为参数传递,因为我们需要不带括号的 substring )。
To split each group, we can use either Pattern.splitAsStream()
or Arrays.split()
.要拆分每个组,我们可以使用Pattern.splitAsStream()
或Arrays.split()
。 In this case, the latter is preferred because splitting a string on dashes "-"
can be done without compiling a regular expression and utilizing regex engine.在这种情况下,后者是首选,因为无需编译正则表达式和使用正则表达式引擎即可在破折号"-"
上拆分字符串。 Pattern.splitAsStream()
might be more beneficial when array is large ( splitAsStream()
generates a stream directly from the regex engine with need to allocate an intermediate array), or we need to split a string using a regex, and not a regular string like "-"
.当数组很大时, Pattern.splitAsStream()
可能更有益( splitAsStream()
直接从需要分配中间数组的正则表达式引擎生成 stream),或者我们需要使用正则表达式而不是常规字符串来拆分字符串像"-"
。
String parameter = "<1-2-3><4-5-6><7-8-9>";
int[][] matrix = ANGLE_BRACKETS1.matcher(parameter).results()
.map(matchResult -> matchResult.group(1))
.map(str -> Arrays.stream(str.split("-")).mapToInt(Integer::parseInt).toArray())
.toArray(int[][]::new);
System.out.println(Arrays.deepToString(matrix));
Output: Output:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.