多行字符串转数组
Multi-line string to array
我有一个多行字符串:
40 40 40
100 100 100
200 200 200
100 50 200 100
150 150 150
50 60 70 80 90
我需要它作为二维数组。我试图通过 split、guava Splitter 和更多技术来做到这一点,但它仍然不想工作。
public void readTextFile() throws IOException {
content = new String(Files.readAllBytes(Paths.get("/home/cosaquee/dane.txt")));
Splitter niceCommaSplitter = Splitter.on('\n').omitEmptyStrings().trimResults();
Iterable<String> tokens2 = niceCommaSplitter.split(content);
for(String token: tokens2){
boolean atleastOneAlpha = token.matches(".*[a-zA-Z]+.*");
if (!atleastOneAlpha) {
arrayList.add(token);
System.out.println(token);
}
}
}
这就是我现在的代码。我的每一行都有数组列表,但我不知道如何将它变成二维数组。我尝试了很好的旧 for
s 但不知道如何遍历每个字符串并将它们拆分并保存到数组。
为什么要使用拆分器? String 带有 split() 方法。另外,只需使用双循环来填充二维数组。
public String[][] readTextFile() throws IOException {
String content = new String(Files.readAllBytes(Paths.get("yourpath.txt")));
// get the lines
String[] lines = content.split("\r?\n"); // split on new lines
// get the max amt of nums in the file in a single line
int maxInLine = 0;
for (String x : lines) {
String[] temp = x.split("\s+"); // split on whitespace
if (temp.length > maxInLine) {
maxInLine = temp.length;
}
}
String[][] finalArray = new String[lines.length][maxInLine]; // declare and instantiate the array of arrays
// standard double for loop to fill up your 2D array
for (int i = 0; i < lines.length; i++) {
String[] temp = lines[i].split("\s+"); // split on whitespace
for (int j = 0; j < temp.length; j++) {
finalArray[i][j] = temp[j];
}
}
return finalArray;
}
使用 Guava,您可以在单行中生成一个列表列表——为每一行应用一个 Function
,将包含以空格分隔的列作为输入的 String
并输出行的列为 List<String>
。如果您更喜欢二维字符串数组,那么您需要更多代码:
public void readTextFile() throws IOException {
String content = new String(Files.readAllBytes(Paths
.get("/home/cosaquee/dane.txt")));
// convert the string into a list of lists, corresponding to a 2d string array
List<List<String>> twoDimensionalList = Lists
.transform(Splitter.on(System.lineSeparator()).splitToList(content),
new Function<String, List<String>>() {
@Override
public List<String> apply(String row) {
return Splitter.on(" ").splitToList(row);
}
});
// convert the list of lists into a 2d array
String[][] twoDimensionalArray = new String[twoDimensionalList.size()][];
for (int i = 0; i < twoDimensionalArray.length; i++) {
twoDimensionalArray[i] = twoDimensionalList.get(i).toArray(
new String[twoDimensionalList.get(i).size()]);
}
// assert that we got it right
for (String[] row : twoDimensionalArray) {
for (String col : row) {
System.out.print(col + " ");
}
System.out.println();
}
}
我有一个多行字符串:
40 40 40
100 100 100
200 200 200
100 50 200 100
150 150 150
50 60 70 80 90
我需要它作为二维数组。我试图通过 split、guava Splitter 和更多技术来做到这一点,但它仍然不想工作。
public void readTextFile() throws IOException {
content = new String(Files.readAllBytes(Paths.get("/home/cosaquee/dane.txt")));
Splitter niceCommaSplitter = Splitter.on('\n').omitEmptyStrings().trimResults();
Iterable<String> tokens2 = niceCommaSplitter.split(content);
for(String token: tokens2){
boolean atleastOneAlpha = token.matches(".*[a-zA-Z]+.*");
if (!atleastOneAlpha) {
arrayList.add(token);
System.out.println(token);
}
}
}
这就是我现在的代码。我的每一行都有数组列表,但我不知道如何将它变成二维数组。我尝试了很好的旧 for
s 但不知道如何遍历每个字符串并将它们拆分并保存到数组。
为什么要使用拆分器? String 带有 split() 方法。另外,只需使用双循环来填充二维数组。
public String[][] readTextFile() throws IOException {
String content = new String(Files.readAllBytes(Paths.get("yourpath.txt")));
// get the lines
String[] lines = content.split("\r?\n"); // split on new lines
// get the max amt of nums in the file in a single line
int maxInLine = 0;
for (String x : lines) {
String[] temp = x.split("\s+"); // split on whitespace
if (temp.length > maxInLine) {
maxInLine = temp.length;
}
}
String[][] finalArray = new String[lines.length][maxInLine]; // declare and instantiate the array of arrays
// standard double for loop to fill up your 2D array
for (int i = 0; i < lines.length; i++) {
String[] temp = lines[i].split("\s+"); // split on whitespace
for (int j = 0; j < temp.length; j++) {
finalArray[i][j] = temp[j];
}
}
return finalArray;
}
使用 Guava,您可以在单行中生成一个列表列表——为每一行应用一个 Function
,将包含以空格分隔的列作为输入的 String
并输出行的列为 List<String>
。如果您更喜欢二维字符串数组,那么您需要更多代码:
public void readTextFile() throws IOException {
String content = new String(Files.readAllBytes(Paths
.get("/home/cosaquee/dane.txt")));
// convert the string into a list of lists, corresponding to a 2d string array
List<List<String>> twoDimensionalList = Lists
.transform(Splitter.on(System.lineSeparator()).splitToList(content),
new Function<String, List<String>>() {
@Override
public List<String> apply(String row) {
return Splitter.on(" ").splitToList(row);
}
});
// convert the list of lists into a 2d array
String[][] twoDimensionalArray = new String[twoDimensionalList.size()][];
for (int i = 0; i < twoDimensionalArray.length; i++) {
twoDimensionalArray[i] = twoDimensionalList.get(i).toArray(
new String[twoDimensionalList.get(i).size()]);
}
// assert that we got it right
for (String[] row : twoDimensionalArray) {
for (String col : row) {
System.out.print(col + " ");
}
System.out.println();
}
}