【问题标题】:Turning a string into multiple arrays?把一个字符串变成多个数组?
【发布时间】:2013-05-21 21:36:35
【问题描述】:

我是一名正在学习java的学生(今年刚开始。我刚毕业,所以我也无法向我的高中老师寻求帮助),我需要一些关于如何将字符串变成多个字符串的帮助数组(?)。

但是,在此之后我被卡住了(因为我找不到为我的目的分解字符串的方法)。

文件是这样读取的

输入: 第一行是病例数。对于每种情况 第一行是苏珊的工作时间表,第二行是 于尔根的工作时间表。工作时间表将是日期 他们工作的月份(1-30)。第三行将是大小 初始电视。

有问题的文件:

3 2 5 10 2 4 10 60 2 4 10 2 5 10 60 1 2 3 4 7 8 10 12 14 16 18 20 24 26 28 30 1 2 3 6 9 10 17 19 20 21 22 23 25 27 28 29 20

我不知道该怎么做。我试过.split(),但这似乎只适用于字符串的第一行。非常感谢您提供的任何帮助/提示!

【问题讨论】:

  • 您希望它如何组织?每组 3 个的二维数组?
  • 是的。这是基本的想法。第一个数字表示我需要一对数组的数量(模式为计划、计划、大小)。我假设这至少是最简单的方法。

标签: java arrays string file bufferedreader


【解决方案1】:

您可以逐行读取您的输入,然后对其应用正则表达式以将数字与字符串分开,例如:

String numbers = "1 2 3 4 7 8 10 12 14 16 18 20 24 26 28 30";
String[] singleNumbers = numbers.split(" ");

那么您将在 singleNumbers 数组中使用空格分隔这些数字

【讨论】:

    【解决方案2】:

    根据您阅读文件的方式,您可能会使用以下内容:

    BufferedReader in = new BufferedReader(new FileReader("foo.in"));
    

    然后,逐行读取文件:

    String theLine = in.readLine();
    

    现在您有了与该行相对应的字符串(在本例中为第一行,但您可以一遍又一遍地调用readLine() 来读取它们),但是请注意您没有读取“整个”文件正如我认为您所建议的那样,一次获取所有字符串,而不是读者一次一行。

    因此,如果您希望将所有行保存在一个数组中,您需要做的就是声明该数组,然后将每一行读入其中。比如:

    // Declare an array to put the lines into
    int numberOfLines = 10;
    String[] arrayOfStrings = new String[numberOfLines];
    
    // Read the first line
    String aLine = in.readLine();
    
    int i = 0;
    
    // If the line that has been read is null, it's reached the end of the file
    // so stop reading
    while(aLine != null){
        arrayOfStrings[i] = aLine;
        i++;
        aLine = in.readLine();
    }
    
    // arrayOfStrings elements are now each line as a single String!
    

    当然,你可能不知道第一种情况下要读取的文件有多少行,所以声明数组的大小是很困难的。然后,您可以查看动态扩展的数据结构,例如 ArrayList,但这是另一回事。

    【讨论】:

    • "请注意,您不会像我认为您建议的那样一次读取“整个”文件来获取所有字符串,而是读者一次读一行。"喔好吧。我是这么说的;我已经读过文件,之后我需要分解字符串。不过,你的方法奏效了。谢谢!
    【解决方案3】:

    嘿,我为你找到了一个很好的解决方案。

    只需创建一个类来存储每个学生的数据,例如

      import java.util.List;
    
      public class Student {
    
    private int noOfCases;
    private List<String> workSchedule;
    private List<String> initialTV;
    
    //getter setters
    }
    

    那么这个……

    public static void main(String[] args) {
    
        //3 students for reading 9 lines
        //Susan,  Jurgen and You ;)
        Student[] students = new Student[3];
    
        int linesRead = 0;
    
        String aLine = null;
    
        // read each line through aLine
        for (Student student : students) {
    
            //use buffered/scanner classes for reading input(each line) in aLine
            while (aLine != null) {
                ++linesRead;
                if (linesRead == 1) {
                    student.setNoOfCases(Integer.valueOf(aLine));
                    ++linesRead;
                } else if (linesRead == 2) {
                    student.setWorkSchedule(Arrays.asList(aLine.split(" ")));
                    ++linesRead;
                } else if (linesRead == 3) {
                    student.setInitialTV(Arrays.asList(aLine.split(" ")));
                } else {
                    linesRead = 0;
                }
            }
        }
    }
    }
    

    如果您需要阅读更多行/更多学生记录,只需调整学生数组的大小!

    【讨论】:

    • 并将 String 数组转换为 int 数组 :)
    【解决方案4】:

    鉴于您已经有了BufferedReader,您可以尝试这样的方法来为您提供行和列的多维数组:

      BufferedReader reader = ...;
      List<String[]> lines = new ArrayList<String[]>();
      String input;
      while((input = reader.readLine()) != null){
        if (!input.trim().isEmpty()){
          lines.add(input.split(" "));
        }
      }
    
      String[][] data = lines.toArray(new String[lines.size()][]);
    

    【讨论】:

      猜你喜欢
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 2019-12-29
      相关资源
      最近更新 更多