[英]Issue Reading data input files from IntelliJ (as a run configuration or from Terminal) from an imported Maven Project
在審查 Robert Sedgewick 博士和 Kevin Wayne 博士通過他們的書和網站指示的聯合查找算法時:
https://algs4.cs.princeton.edu/10fundamentals/
我使用以下 pom.xml 創建了一個 Maven 項目(我將其導入 IntelliJ IDEA):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu.princeton.cs.algs4</groupId>
<artifactId>algs4</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<show>private</show> <!--javadoc shows all classes and members-->
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<stylesheetfile>${basedir}/src/main/javadoc/stylesheet.css</stylesheetfile>
<show>public</show>
</configuration>
</plugin>
</plugins>
</reporting>
</project>
項目文件結構:
algs4
|
├── pom.xml
└── src
├── main
│ ├── java
│ │ └── edu
│ │ └── princeton
│ │ └── cs
│ │ └── algs4
│ │ ├── QuickFindUF.java
│ │ ├── QuickUnionUF.java
│ │ ├── StdIn.java
│ │ ├── StdOut.java
│ │ └── WeightedQuickUnionUF.java
│ └── resources
│ ├── largeUF.txt
│ ├── mediumUF.txt
│ └── tinyUF.txt
└── test
└── java
快速查找UF.java:
public class QuickFindUF {
private int[] id; // id[i] = component identifier of i
private int count; // number of components
public QuickFindUF(int n) {
count = n;
id = new int[n];
for (int i = 0; i < n; i++)
id[i] = i;
}
public int count() {
return count;
}
public int find(int p) {
validate(p);
return id[p];
}
// validate that p is a valid index
private void validate(int p) {
int n = id.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));
}
}
public void union(int p, int q) {
validate(p);
validate(q);
int pID = id[p]; // needed for correctness
int qID = id[q]; // to reduce the number of array accesses
// p and q are already in the same component
if (pID == qID) return;
for (int i = 0; i < id.length; i++)
if (id[i] == pID) id[i] = qID;
count--;
}
public static void main(String[] args) {
int n = StdIn.readInt();
QuickFindUF uf = new QuickFindUF(n);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.find(p) == uf.find(q)) continue;
uf.union(p, q);
StdOut.println(p + " " + q);
}
StdOut.println(uf.count() + " components");
}
}
QuickUnionUF.java:
public class QuickUnionUF {
private int[] parent; // parent[i] = parent of i
private int count; // number of components
public QuickUnionUF(int n) {
parent = new int[n];
count = n;
for (int i = 0; i < n; i++) {
parent[i] = i;
}
}
public int count() {
return count;
}
public int find(int p) {
validate(p);
while (p != parent[p])
p = parent[p];
return p;
}
// validate that p is a valid index
private void validate(int p) {
int n = parent.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));
}
}
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
parent[rootP] = rootQ;
count--;
}
public static void main(String[] args) {
int n = StdIn.readInt();
QuickUnionUF uf = new QuickUnionUF(n);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.find(p) == uf.find(q)) continue;
uf.union(p, q);
StdOut.println(p + " " + q);
}
StdOut.println(uf.count() + " components");
}
}
加權QuickUnionUF.java:
public class WeightedQuickUnionUF {
private int[] parent; // parent[i] = parent of i
private int[] size; // size[i] = number of elements in subtree rooted at i
private int count; // number of components
public WeightedQuickUnionUF(int n) {
count = n;
parent = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
public int count() {
return count;
}
public int find(int p) {
validate(p);
while (p != parent[p])
p = parent[p];
return p;
}
// validate that p is a valid index
private void validate(int p) {
int n = parent.length;
if (p < 0 || p >= n) {
throw new IllegalArgumentException("index " + p + " is not between 0 and " + (n-1));
}
}
public void union(int p, int q) {
int rootP = find(p);
int rootQ = find(q);
if (rootP == rootQ) return;
// make smaller root point to larger one
if (size[rootP] < size[rootQ]) {
parent[rootP] = rootQ;
size[rootQ] += size[rootP];
}
else {
parent[rootQ] = rootP;
size[rootP] += size[rootQ];
}
count--;
}
public static void main(String[] args) {
int n = StdIn.readInt();
WeightedQuickUnionUF uf = new WeightedQuickUnionUF(n);
while (!StdIn.isEmpty()) {
int p = StdIn.readInt();
int q = StdIn.readInt();
if (uf.find(p) == uf.find(q)) continue;
uf.union(p, q);
StdOut.println(p + " " + q);
}
StdOut.println(uf.count() + " components");
}
}
可以在以下位置找到 StdIn 的實現:
https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/StdIn.java.html
可以在以下位置找到 StdOut 的實現:
https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/StdOut.java.html
數據文件可以在以下位置找到:
當我進行mvn clean install
或mvn test
它創建了適當的目標目錄,如下所示:
target
├── classes
├── edu
│ └── princeton
│ └── cs
│ └── algs4
│ ├── QuickFindUF.class
│ ├── QuickUnionUF.class
│ ├── StdIn.class
│ ├── StdOut.class
│ └── WeightedQuickUnionUF.class
├── largeUF.txt
├── mediumUF.txt
└── tinyUF.txt
因為,我將它作為 maven 項目創建並導入到 IntelliJ 中,所以我無法弄清楚如何設置“作為獨立 Java 應用程序運行”(將 txt 文件作為 main() 方法的參數。
所以,我所做的是發出“mvn 測試”
什么都沒發生……有沒有辦法通過終端或 IntelliJ IDEA 做到這一點?
要在終端中運行,請使用java edu.princeton.cs.algs4.QuickFindUF < tinyUF.txt
或cat tinyUF.txt | java edu.princeton.cs.algs4.QuickFindUF
cat tinyUF.txt | java edu.princeton.cs.algs4.QuickFindUF
。
要在 IntelliJ IDEA 中運行,請使用重定向輸入選項。 請注意,路徑是相對於工作目錄的。
我看不到 Crazy Coder 引用的應用程序配置
您可以通過單擊 + 按鈕在 IntelliJ IDEA 中創建新的應用程序或 Jar 應用程序配置。 有關詳細信息,請參閱幫助。
當我嘗試這樣做時,它找不到位於目標目錄中的 jar 文件。
運行mvn package
目標后,目標目錄中將提供 JAR 文件。 您可以從命令行 Maven 或IDE 執行此操作。 還可以設置觸發器以在運行/調試之前構建 jar 或將此 Maven 目標添加到運行/調試配置的Before Launch 步驟。
另請參閱我對類似問題的其他回答。 它建議了另一種使用包裝類的方法,無需重定向選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.