代码之家  ›  专栏  ›  技术社区  ›  Abby

遍历字典数组

  •  0
  • Abby  · 技术社区  · 7 年前

    我有一个字符串数组,其中包含一首故意拼写错误的诗。我试图通过将字符串数组与包含字典的字符串数组进行比较来迭代字符串数组以识别拼写错误。如果可能的话,我希望得到一个建议,允许我继续使用嵌套for循环

     for (int i = 0; i < poem2.length; i++) {
            boolean found = false;
            for (int j = 0; j < dictionary3.length; j++) {
                if (poem2[i].equals(dictionary3[j])) {
                    found = true;
                    break;
                }
            }
            if (found==false) {
                System.out.println(poem2[i]);
            }
        }
    

    输出是打印出拼写正确的单词和拼写错误的单词,我的目标是只打印出拼写错误的单词。下面是我如何填充“dictionary3”和“poem2”数组的:

          char[] buffer = null;
          try {
            BufferedReader br1 = new BufferedReader(new 
        java.io.FileReader(poem));
            int bufferLength = (int) (new File(poem).length());
            buffer = new char[bufferLength];
            br1.read(buffer, 0, bufferLength);
            br1.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    
        String text = new String(buffer);
        String[] poem2 = text.split("\\s+");
    
        char[] buffer2 = null;
        try {
            BufferedReader br2 = new BufferedReader(new java.io.FileReader(dictionary));
            int bufferLength = (int) (new File(dictionary).length());
            buffer2 = new char[bufferLength];
            br2.read(buffer2, 0, bufferLength);
            br2.close();
        } catch (IOException e) {
            System.out.println(e.toString());
        }
    
        String dictionary2 = new String(buffer);
        String[] dictionary3 = dictionary2.split("\n");
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   Pshemo    7 年前

    你的基本问题是一致的

    String dictionary2 = new String(buffer);
    

    您正在尝试转换表示存储在中的词典的字符 buffer2 但你用过 buffer (无 2 后缀)。这种命名变量的方式可能表明您需要一个循环,或者在本例中需要一个单独的方法,该方法将为所选的文件数组返回它所包含的单词(您还可以添加作为方法参数分隔符的字符串)。

    所以你的 dictionary2 保留的字符来自 缓冲器 它代表的是诗歌,而不是字典数据。

    另一个问题是

    String[] dictionary3 = dictionary2.split("\n");
    

    因为你只在 \n 但有些操作系统(如Windows)使用 \r\n 作为行分隔符序列。因此,字典数组可能包含以下单词 foo\r 而不是 foo 这将导致 poem2[i].equals(dictionary3[j] 总是失败。

    要避免此问题,您可以在上拆分 \\R (自Java 8起提供)或 \r?\n|\r .


    代码中还有其他问题,如关闭try部分中的资源。如果之前将引发任何异常, close() 将永远不会在未关闭资源的情况下调用。要解决此问题,请在中关闭资源 finally 节(总是在尝试后执行-无论是否引发异常),或更好地使用 try-with-resources .


    顺便说一句,您可以简化/澄清负责从文件中读取单词的代码

    List<String> poem2 = new ArrayList<>();
    Scanner scanner = new Scanner(new File(yourFileLocation));
    while(scanner.hasNext()){//has more words
        poem2.add(scanner.next());
    }
    

    用于字典而不是 List 您应该使用 Set/HashSet 避免重复(通常在检查集合是否包含某些元素时,集合也具有更好的性能)。此类集合已经提供了如下方法 contains(element) 所以你不需要那个内环。

        2
  •  0
  •   Keara    7 年前

    我复制了你的代码并运行了它,我注意到了两个问题。好消息是,这两种方法都是快速解决方案。

    #1 当我在 dictionary3 读入后,它与中的所有内容完全相同 poem2 . 您在字典中阅读的代码中的这一行是问题所在:

     String dictionary2 = new String(buffer);
    

    您正在使用 buffer ,这是你在诗中读到的变量。因此,缓冲区中包含的诗歌与您的诗歌和词典的结尾相同。我想你想用 buffer2 相反,这是你过去在字典里读到的:

     String dictionary2 = new String(buffer2);
    

    当我改变这一点时,字典和诗似乎有正确的词条。

    #2 另一个问题,正如Pshemo在他们的回答中指出的那样(这是完全正确的,也是一个非常好的答案!)就是你要分开 \n 为了字典。我想说的唯一不同于Pshemo的是,你可能应该 \\s+ 就像你写诗一样,保持一致。事实上,当我调试时,我注意到字典中的单词都在末尾附加了“\r”,可能是因为您正在拆分 \n个 . 要解决此问题,请更改此行:

    String[] dictionary3 = dictionary2.split("\n");
    

    对此:

    String[] dictionary3 = dictionary2.split("\\s+");
    

    尝试更改这两行,并让我们知道这是否解决了您的问题。祝你好运!

        3
  •  -1
  •   Brandon Miller    7 年前

    将字典转换为ArrayList并使用 Contains 相反

    像这样的方法应该会奏效:

    if(dictionary3.contains(poem2[i])
       found = true;
    else
       found = false;
    

    使用此方法还可以摆脱该嵌套循环,因为contains方法会为您处理该循环。

    可以使用以下方法将字典转换为ArrayList: new ArrayList<>(Arrays.asList(array))