【问题标题】:Doesn't save the words in array不将单词保存在数组中
【发布时间】:2020-07-30 23:50:21
【问题描述】:

我有一个简单的问题。我尝试读取文件,我想将每个单词添加到我的数组“短语”中。问题出现在 for 循环中。我得到了异常“索引 0 超出长度 0 的范围”。 你能帮我吗?

    String [] tokens;
    String line;
    String hash = " ";
    int n = 0;
    String [] phrase = new String [n];

    public void loadFile()
    {
        try
        {
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(new FileReader("z3data1.txt"));

            while((line = br.readLine()) != null)
            {
                tokens = line.split("[ ]");
                n += tokens.length;
            }
            for(int j = 0; j<tokens.length; j++)
            {
                phrase[j] = tokens[j];
            }
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
   }

【问题讨论】:

  • 您将 n 设置为 0,因此短语的长度也为 0。如果您想要可变长度的内容,请使用 ArrayList。
  • 好的,谢谢。但是在while循环中增加n是错误的吗?
  • 不,虽然我不明白它的意义

标签: java arrays string file exception


【解决方案1】:

您将 n 设置为 0,因此当您说 String[] phrase = String[n] 时,短语的长度也为 0。因此,您不能向其中添加任何内容。

如果你想要一些可变长度的东西,你可以使用 ArrayList。在下面的代码中,您可以直接使用 Collections.addAll 将行拆分,并将所有内容放入短语 ArrayList 中。

    String line;
    //Note that you can get rid of tokens here, since it's being inlined below
    ArrayList<String> phrase = new ArrayList<>();

    public void loadFile()
    {
        try
        {
            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(new FileReader("z3data1.txt"));

            while((line = br.readLine()) != null)
            {
                //No need for a for-loop below, you can do everything in one line
                Collections.addAll(phrase, line.split("[ ]"));
            }
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
   }

【讨论】:

    【解决方案2】:

    一些观察。

    • 您收到错误是因为您的数组不够大
      并且索引j 超出了它的大小。
    • 您不断覆盖while loop 中的令牌。 while 循环需要包含将标记复制到短语数组的过程。

    所以尝试以下方法:

          while((line = br.readLine()) != null) {
                  tokens = line.split("[ ]");
                  n += tokens.length; // don't really need this.
              //starting offset to write into phrase
              int len = phrase.length;
              phrase = Arrays.copyOf(phrase,phrase.length + tokens.length);
    
              for(int j = 0; j<tokens.length; j++) {
                  phrase[j + len] = tokens[j];
              }
           }
    

    此声明

    phrase = Arrays.copyOf(phrase,phrase.length + tokens.length)
    

    复制短语的内容并增加数组大小以处理标记的写入。

    另一种(可能是首选)替代方法是使用List&lt;String&gt;,它会随着您的需要而增长。

    List<String> phrase = new ArrayList<>();
    
    for(int j = 0; j<tokens.length; j++) {
           phrase.add(tokens[j]);
    }
    // or skip the loop and just do
    Collections.addAll(phrase,tokens);
    

    一个观察。我不知道你在分裂什么,但你的分裂声明看起来很可疑。

    【讨论】:

      猜你喜欢
      • 2020-02-14
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      相关资源
      最近更新 更多