学习工具:Eclipse Java2018-09(win10 64位)
下载地址:https://www.eclipse.org/downloads/
学习环境:java https://www.java.com/zh_CN/
第一章 基础
1.1 基础编程模型
讲了java的一些语法知识:数据类型,语句,数组,静态方法,字符串,标准输入/输出。
在最后的二分法查找的例子,遇到些问题:运行出错。
解决的步骤:1.下载algs4.jar ,algs4-data.zip
2.将algs4.jar导入到Eclipse
3.将algs4-data.zip解压到src的同目录下
4.代码改为:
import java.util.Arrays;
import edu.princeton.cs.algs4.*;
public class BinarySearch {
public static int indexOf(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
}
public static int rank(int key, int[] a) {
return indexOf(a, key);
}
public static void main(String[] args) {
In in = new In(args[0]);
int[] whitelist = in.readAllInts();
In in2 = new In(args[1]);
int[] whitelist2 = in2.readAllInts();
// sort the array
Arrays.sort(whitelist);
// read integer key from standard input; print if not in whitelist
for (int i =0;i<whitelist2.length;i++) {
int key = whitelist2[i];
if (BinarySearch.indexOf(whitelist, key) == -1)
StdOut.println(key);
}
}
5.打开run configuration界面,在Arguments界面中 Program arguments写入
tinyW.txt tinyT.txt
6.run运行程序