浅草夏洛洛 2019-10-19 11:32 采纳率: 100%
浏览 829
已采纳

关于算法第四版StdIn.readAllStrings()的输入问题

编译器:eclipse
最近学习算法第四版,在写代码过程中发现输入按了enter之后也没有反应。
已经安装好了algs4库。
找到最新的选择排序并运行:https://algs4.cs.princeton.edu/21elementary/Selection.java.html

在运行时按了enter也没有反应。

图片说明

查看readallstrings源码也没有分析出原因了。

/**
     * Reads all remaining tokens from this input stream and returns them as
     * an array of strings.
     *
     * @return all remaining tokens in this input stream, as an array of strings
     */
    public String[] readAllStrings() {
        // we could use readAll.trim().split(), but that's not consistent
        // since trim() uses characters 0x00..0x20 as whitespace
        String[] tokens = WHITESPACE_PATTERN.split(readAll());
        if (tokens.length == 0 || tokens[0].length() > 0)
            return tokens;
        String[] decapitokens = new String[tokens.length-1];
        for (int i = 0; i < tokens.length-1; i++)
            decapitokens[i] = tokens[i+1];
        return decapitokens;
    }

求解,感激不尽。

  • 写回答

3条回答 默认 最新

  • 毕小宝 领域专家: 后端开发技术领域 2019-10-19 11:53
    关注

    是的,它那个读取数据的有问题,并没有看到输入结束。不用它封装的 StdIn 和 StdOut 类,只用排序算法,算法是正确的。

            String[] a = {"as","d","asd","as","d"};
            System.out.println("Before sorted:"+Arrays.toString(a));
            Selection.sort(a);
            System.out.println("after sorted:"+Arrays.toString(a));
    

    运行结果为:

    Before sorted:[as, d, asd, as, d]
    after sorted:[as, as, asd, d, d]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?