昨天周五上算法实验吃了一大亏,用的是普林斯顿的算法JAR包!发现原来用Myeclipse的导入JAR包的傻瓜操作出现了大问题。导入了就不发会用的状态!最后老师还批评了了一顿说:“你们根本就不了解JAVA,不知到什么是JAR,什么是JDK!”
哇,什么鬼,因此特做此记录,来记录一下我今天研究的成果!
首先,得明白这个JAR包里面到底是什么?
下载的是普林斯顿的 algs4.jar !用什么就查什么,以前看别人在用什么JAVAEE的使用指南(什么方法该怎么用,该怎么导入),感觉挺好!不过,那个还是不如看源码来的清楚明白!所以,就看看这个神秘的源码!其实用什么editer看都一样哈,下面以Myeclipse为例子吧!
顺便也记录下怎么直接 import JAR包就能用的方法吧!
myeclipse导入algs4.jar以及使用:
我创建了以下的一个普通JAVA工程:
右击src->import->Archive File->Next->然后浏览找到你JAR包的位置,例如我的是(最后finish就可以了):
最后src里面是这个样子的:
这个方法的原理呢我认为是相当于将JAR包直接拷贝过来,里面的类(已经提前写好)你就可以直接调用就行了,很方便!
然后就能直接看源码,明白到底这些JAR包是干什么的!
附:普林斯顿算法代码地址:https://algs4.cs.princeton.edu/code/?spm=a2c4e.11153940.blogcont620164.11.5fb1449dFr31KG
那怎么使用呢:下面以为生成随机数例子吧:
打开RandomSeq.java:
这些英文其实在cmd模式下的运行指令(下面有简单介绍cmd该怎么用),如果你不用这个,感觉没什么用,所以,我就直接说在Myeclipse该怎么用:
/******************************************************************************
* Compilation: javac RandomSeq.java
* Execution: java RandomSeq n lo hi
* Dependencies: StdOut.java
*
* Prints N numbers between lo and hi.
*
* % java RandomSeq 5 100.0 200.0
* 123.43
* 153.13
* 144.38
* 155.18
* 104.02
*
******************************************************************************/
package edu.princeton.cs.algs4;
/**
* The {@code RandomSeq} class is a client that prints out a pseudorandom
* sequence of real numbers in a given range.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/11model">Section 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class RandomSeq {
// this class should not be instantiated
private RandomSeq() { }
/**
* Reads in two command-line arguments lo and hi and prints n uniformly
* random real numbers in [lo, hi) to standard output.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
// command-line arguments
int n = Integer.parseInt(args[0]);
// for backward compatibility with Intro to Programming in Java version of RandomSeq
if (args.length == 1) {
// generate and print n numbers between 0.0 and 1.0
for (int i = 0; i < n; i++) {
double x = StdRandom.uniform();
StdOut.println(x);
}
}
else if (args.length == 3) {
double lo = Double.parseDouble(args[1]);
double hi = Double.parseDouble(args[2]);
// generate and print n numbers between lo and hi
for (int i = 0; i < n; i++) {
double x = StdRandom.uniform(lo, hi);
StdOut.printf("%.2f\n", x);
}
}
else {
throw new IllegalArgumentException("Invalid number of arguments");
}
}
}
/******************************************************************************
* Copyright 2002-2018, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
其实里面代码很简单,就只有一个主函数,所以,就import直接用就好了。我就直接新建一个class调用这个类就OK了。
下面是我自己的代码:
package it.order;
import edu.princeton.cs.algs4.*;
public class BornNum {
public static void main(String[] args) {
String[] a={"100000000","1","1000"};//随机生成1亿个1~1000的数
RandomSeq.main(a);
}
}
编译运行,你就会看到了结果了!
CMD命令行下运行:
上面说到,那么普林斯顿源码就鼓励大家用CMD去运行,更好理解JAVA的运行机制,所以,下面就简单介绍一下怎么用CMD(CMD就特别的方便快捷)
打开CMD,记住你的algs4.jar和你的java文件的绝对路径。我的两个就是直接放在桌面下面了:
然后就是下面:
注意:上面是没有用args的情况下使用的,附一张用args使用的截图:
到这里就快接近尾声了,但有个值得思考的问题:
为什么cmd都那么简单的就能实现JAVA的文件编译,为什么还要IDE呢,用IDE就差不多跟晕车一样,还得熟悉IDE该怎么去用。就像我这几天学习IDEA一样,晕头转向的。。。但是里面的打包以及导入是及其的方便啊,还有上面WEB Model!哇~~~