多行字符串到数组

时间:2015-06-01 19:25:30

标签: java arrays guava

我有一个多行字符串:

40 40 40 
100 100 100
200 200 200
100 50 200 100
150 150 150
50 60 70 80 90

我需要它作为2D数组。我试图通过拆分,番石榴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);
            }
        }
    }

这是我现在的代码。我有每行的arraylist但我不知道如何使它成为2D数组。我尝试了很好的旧for但是不知道如何迭代每个字符串并将它们拆分并保存到数组中。

2 个答案:

答案 0 :(得分:3)

为什么要使用Splitter? String附带一个split()方法。另外,只需使用双循环来填充你的2d数组。

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;
}

答案 1 :(得分:0)

使用Guava,您可以在单行中生成列表列表 - 为每行应用Function作为输入,String包含以空格分隔的列并输出列行List<String>。如果您更喜欢2d字符串数组,那么您需要更多代码:

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();
    }
}