配置《算法 第四版》的Eclipse开发环境

时间:2023-03-08 23:58:16
配置《算法 第四版》的Eclipse开发环境

1、 安装JAVA

JAVA网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html

配置环境变量(我把JAVA安装在路径:F:\Java\jdk1.8):

PATH=.;%JAVA_HOME%\bin
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\toos.jar;
JAVA_HOME=F:\Java\jdk1.8

检查JAVA是否安装成功(出现如下的信息则表示JAVA安装完成):

配置《算法 第四版》的Eclipse开发环境

配置《算法 第四版》的Eclipse开发环境

配置《算法 第四版》的Eclipse开发环境

2、 下载相关文件

打开普林斯顿大学网站:http://algs4.cs.princeton.edu/code/

分别点击下载两个文件(algs4.jar和algs4-data.zip)

配置《算法 第四版》的Eclipse开发环境

重点:(我当时因为没有按要求配置该文件,导致运行所有的官网下载的程序都失败,统一提示为:错误: 找不到或无法加载主类)

在该页面的下面可以找到如下这段话:

配置《算法 第四版》的Eclipse开发环境

说明了要把下载的algs4.jar文件存放到如下文件夹:C:\Users\Kylin Lin\algs4(注意:将Kylin Lin换成你的用户名),然后将该文件的路径添加到刚才的JAVA环境变量classpath中,所以完整的classpath路径应该是这样的:

.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\toos.jar;C:\Users\Kylin Lin\algs4\algs4.jar;
 

3、 配置Eclipse

Eclipse下载地址:http://www.eclipse.org/downloads/

安装时选择第一个选项即可

配置《算法 第四版》的Eclipse开发环境

安装完毕后新建JAVA工程(我这里命名为Algorithms),添加刚才下载的algs4.jar文件,否则不能使用书中代码的自定义库

配置《算法 第四版》的Eclipse开发环境

配置《算法 第四版》的Eclipse开发环境

至此,开发环境配置完毕,为了方便,把下载的另一个文件algs4-data.zip解压到该工程的src文件夹中

4、 测试

在该工程中新建类BinarySearch
package chapter1; /****************************************************************************** * Compilation: javac chapter1\BinarySearch.java * Execution: java chapter1.BinarySearch tinyW.txt < tinyT.txt * Data files: http://www.cs.princeton.edu/introcs/43sort/emails.txt * http://www.cs.princeton.edu/introcs/43sort/whitelist.txt * * Read in an alphabetical list of words from the given file. * Then prompt the user to enter words. The program reports which * words are *not* in the wordlist. * * % java BinarySearch whitelist.txt < emails.html * marvin@spam * mallory@spam * eve@airport * ******************************************************************************/ import java.util.Arrays; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdIn; import edu.princeton.cs.algs4.StdOut; public class BinarySearch { // return the index of the key in the sorted array a[]; -1 if not found public static int search(String key, String[] a) { return search(key, a, 0, a.length); } public static int search(String key, String[] a, int lo, int hi) { // possible key indices in [lo, hi) if (hi <= lo) return -1; int mid = lo + (hi - lo) / 2; int cmp = a[mid].compareTo(key); if (cmp > 0) return search(key, a, lo, mid); else if (cmp < 0) return search(key, a, mid+1, hi); else return mid; } // whitelist, exception filter public static void main(String[] args) { In in = new In(args[0]); String s = in.readAll(); String[] words = s.split("\\s+"); System.err.println("Done reading words"); // sort the words (if needed) Arrays.sort(words); System.err.println("Done sorting words"); // prompt user to enter a word and check if it's there while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (search(key, words) < 0) StdOut.println(key); } } }

得到如下的显示,则表示配置成功,可以运行官方的源程序以及使用官方所自定义的库

配置《算法 第四版》的Eclipse开发环境

细节提示:

1. 当前的运行目录是Algorithms\src,且该目录下需要具有tinyW.txt和tinyT.txt文件

2. 因为我把algs4-data.zip文件解压到了src目录下,所以需要用包来管理源程序,否则会造成混乱,所以在用java命令运行时需要指定包名(在源程序中指定了包名: package chapter1;)

参考我的工程结构

配置《算法 第四版》的Eclipse开发环境

5、 在Eclipse中配置github

参考:http://www.cnblogs.com/yc-755909659/p/3753626.html