一段时间以来,我第一次在 Eclipse 之外进行 Java 编程(对于 coursera 算法课程),并且我正在尝试使用 SBT 进行构建。SBT 工作正常(虽然启动缓慢),但我不知道如何启用断言。以下任何一项似乎都不起作用。
javaOptions += "-ea" // doesn't work
javaOptions in run += "-ea" // doesn't work either
构建.sbt
// disable using the Scala version in output paths and artifacts
crossPaths := false
// Enable assertions?
javaOptions += "-ea" // doesn't work
//javaOptions in run += "-ea" // doesn't work either
organization := "me"
name := "me"
version := "1.0-SNAPSHOT"
// Use jars from parent dir. Normally jars are stuck in lib/
unmanagedJars in Compile += file("../stdlib.jar")
unmanagedJars in Compile += file("../algs4.jar")
快速查找.java
import java.util.Arrays; // I hate java so much
public class QuickFind {
public int[] id;
public QuickFind (int N) {
id = new int[N];
int i;
for (i = 0; i < N; i++) {
id[i] = i;
}
}
public boolean connected (int p, int q) {
return id[p] == id[q];
}
public void union (int p, int q) {
// Walk through array and make everything with id = p || q
// equal to id p
int pid = id[p];
int qid = id[q];
int i;
for (i = 0; i < id.length; i++) {
if (id[i] == qid) id[i] = pid;
}
}
public static void main (String[] args) {
StdOut.println("QuickFind"); // from stdlib.jar
QuickFind uf = new QuickFind(4);
uf.union(0,1);
// Assert unions work
StdOut.println("array=" + Arrays.toString(uf.id));
assert uf.connected(0,1);
assert uf.connected(0,2); // <---------------------this should fail
}
}