起因:在学习《算法第四版》过程中总是遇到一些算法之外的小问题,比如本书中所需要下载的库。今天在学习排序那一节时,发现排序框架main()方法中的In的readStrings()方法不能使用,google了半天也没有找出解决方案,遂自己到algs4这个包里面查看此方法的源码(具体位置为edu.princeton.cs.algs4.In),代码如下:
/**
* Reads all strings from a file and returns them as
* an array of strings.
*
* @param filename the name of the file
* @return the strings in the file
* @deprecated Replaced by {@code new In(filename)}.{@link #readAllStrings()}.
*/
@Deprecated
public static String[] readStrings(String filename) {
return new In(filename).readAllStrings();
}
由注释@deprecated Replaced by {@code new In(filename)}.{@link #readAllStrings()}.
可以看到,这个方法已经被弃用了,因此只要将main()中读取字符串的代码改为String[] a = new In("tiny.txt").readAllStrings();
此时程序就可以正常运行了
完整main()方法如下:
public static void main(String[] args) {
String[] a = new In("tiny.txt").readAllStrings();
System.out.println(a);
sort(a);
assert isSorted(a);
show(a);
}