Eric_cobot

导航

Eclipse导入第四版《算法》algs4库

最近在研究《算法》,遇到algs4库导入eclipse问题,查了很多网站,都不适用,最终解决,特此记录一下。第一次写博客,有什么不足之处望各位大神纠正。

1. 首先打开eclipse软件,然后开始新建项目,File -> New Java Project,项目名随便写(我的是Math),点击确定生成项目,如下图

 

2 在资源管理器中,右键项目,选在新建,包或者类,如下:

 

 

 

 3 创建好类后,右键项目,选择属性->java构建路径->库->类路径->添加外部库:

 

 

4 然后找到你存放algs4.jar的位置,点击应用即可,会在目录下显示引用的库。

 

 

5 使用algs4自带的测试程序如下:

 

/******************************************************************************
* Compilation: javac-algs4 TestAlgs4.java
* Execution: java-algs4 TestAlgs4 n
*
* Simulates the motion of n hard disks, subject to the laws of elastic
* collisions. This program is intended to test that algs4.jar is properly
* installed.
*
******************************************************************************/

import edu.princeton.cs.algs4.CollisionSystem;
import edu.princeton.cs.algs4.Particle;
import edu.princeton.cs.algs4.StdDraw;

public class TestAlgs4 {
public static void main(String[] args) {
int n = 20; // number of particles (default 20)
if (args.length == 1) {
n = Integer.parseInt(args[0]);
}

// enable double buffering to support animations
StdDraw.enableDoubleBuffering();

// create the n particles
Particle[] particles = new Particle[n];
for (int i = 0; i < n; i++) {
particles[i] = new Particle();
}

// simulate the system
CollisionSystem system = new CollisionSystem(particles);
system.simulate(Double.POSITIVE_INFINITY);
}
}

6 运行结果如下:

 

 

 

 

 

 

 

 

 

 

 

......

 

posted on 2021-06-11 15:16  Eric_cobot  阅读(291)  评论(0编辑  收藏  举报