- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 eclipse 中收到了一个令人困惑的错误,但在我们的主服务器上没有从命令行 linux 帐户。所有代码都在 eclipse 的主 src 目录中。代码在命令行编译,但在我的 Mac OS X 笔记本电脑上的 Eclipse 中产生此错误:
Exception in thread "main" java.lang.NoSuchMethodError: Graph: method <init>()V not found
at Lab17.main(Lab17.java:11)
代码
Lab17.java
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
public class Lab17 {
// Lab17 first attempt at creating a graph
public static void main(String[] args) throws Exception {
Graph myGraph = new Graph();
URLConnection conn = null;
try {
URL url = new URL("http://csc.mendocino.edu/~jbergamini/222data/flights/flights");
conn = url.openConnection();
} catch (IOException e) {
System.out.println("Unable to open Flights file");
System.exit(1);
}
Scanner s = null;
try {
s = new Scanner(conn.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while (s.hasNext()) {
String src = s.next();
String dst = s.next();
s.next();
double cost = s.nextDouble();
//System.out.println(src+" "+dst+" "+cost);
myGraph.addEdge(src, dst, cost);
}
System.out.println(myGraph.breadthFirst("Austin", "Washington"));
System.out.println(myGraph.depthFirst("LosAngeles", "Washington"));
System.out.println(myGraph.breadthFirst("LosAngeles", "Washington"));
System.out.println(myGraph.depthFirst("Washington", "LosAngeles"));
System.out.println(myGraph.breadthFirst("Washington", "LosAngeles"));
}
}
Graph.java
import java.util.LinkedList;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.TreeMap;
public class Graph {
private TreeMap<String, Vertex> vertexMap;
/** Creates a new, empty graph */
public Graph() {
// create vertexMap for Graph
vertexMap = new TreeMap<String, Vertex>();
}
/**
* Adds an edge
* @param src the source vertex
* @param dst the destination vertex
* @param cost the weight of the edge
*/
public void addEdge(String src, String dst, double cost) {
//System.out.println(src+" "+dst+" "+cost+" in Graph()");
// check to see if there is a src vertex
if(vertexMap.get(src) == null) {
// if src Vertex does not exist create Vertex for src
vertexMap.put(src, (new Vertex(src)));
//System.out.println(vertexMap.get(src).toString());
}
// check to see if there is a dst Vertex
if(vertexMap.get(dst) == null) {
// if dst Vertex does not exist create Vertex for dst
vertexMap.put(dst, (new Vertex(dst)));
//System.out.println(vertexMap.get(dst).toString());
}
// populate the dst and cost for Edge on the src Vertex vertexMap element
Vertex srdVertex = vertexMap.get(src);
Vertex dstVertex = vertexMap.get(dst);
Edge sEdge = new Edge(dstVertex, cost);
srdVertex.addEdge(sEdge);
}
/** Clears/empties the graph */
public void clear() {
vertexMap = new TreeMap<String,Vertex>();
}
/**
* Traverses, depth-first, from src to dst, and prints the vertex names in the order visited.
* @param src the source vertex
* @param dst the destination vertex
* @return whether a path exists from src to dst
*/
public boolean depthFirst(String src, String dst) {
System.out.println("Depth-first from "+src+" to "+dst);
for(Map.Entry<String,Vertex> entry: vertexMap.entrySet()) {
String key = entry.getKey();
Vertex thisVertex = entry.getValue();
System.out.println(key + " => " + thisVertex.toString());
}
return false;
}
/**
* Traverses, breadth-first, from src to dst, and prints the vertex names in the order visited
* @param src the source vertex
* @param dst the destination vertex
* @return whether a path exists from src to dst
*/
public boolean breadthFirst(String src, String dst) {
System.out.println("Breadth-first from "+src+" to "+dst);
// find starting vertex in vertexMap
Vertex start = vertexMap.get(src);
LinkedList<Vertex> vertexQue = new LinkedList<Vertex>();
LinkedList<Vertex> visitedV = new LinkedList<Vertex>();
// check it for null
if( start == null) {
throw new NoSuchElementException(" Start vertex not found");
}
// create a Queue for searching through vertex edges
//Queue<Vertex> q = new Queue<Vertex>();
vertexQue.add( start );
start.dest = 0;
boolean found = false;
while( !vertexQue.isEmpty() && !found) {
Vertex v = vertexQue.removeLast();
if( v.toString() == dst) {
// print queue
found = true;
}
else if(!visitedV.contains(v)){
// put all the adj vertex's into the queue
for( Edge e: v.getEdges() ) {
Vertex w = e.getDst();
vertexQue.add( w );
}
}
// add v to visitedV linked list
if(!visitedV.contains(v)){
visitedV.add(v);
}
}
System.out.print("[");
for(int i=0; i < visitedV.size(); i++) {
System.out.print(visitedV.get(i)+", ");
}
System.out.println("]");
/*forVertex> entry: vertexMap.entrySet()) {
String key = entry.getKey();
Vertex thisVertex = entry.getValue();
System.out.println(key + " => " + thisVertex.toString());
for(Edge e : thisVertex.getEdges() ){
System.out.print(e.toString());
}
System.out.println();
System.out.println("All Edges Evaluated");
}*/
return false;
}
}
顶点.java
import java.util.Set;
import java.util.TreeSet;
public class Vertex {
private String name;
private TreeSet<Edge> adj;
public double dest;
/**
* Creates a new vertex
* @param name the name of the vertex
*/
public Vertex(String name) {
this.name = name;
adj = new TreeSet<Edge>();
}
public TreeSet<Edge> getEdges() {
return this.adj;
}
/**
* Returns the set of all edges starting from this vertex.
* Set shall be ordered with respect to the names of the destination vertices.
*/
public Set<Edge> allAdjacent() {
Set<Edge> vertexSet = adj;
return null;
}
public String toString() {
return name;
}
public void addEdge(Edge e) {
this.adj.add(e);
}
}
public class Edge implements Comparable<Edge> {
private Vertex dst;
private double cost;
/** Creates a new edge with an associated cost
* @param dst the destination vertex
* @param cost the weight of the edge
*/
public Edge(Vertex dst, double cost) {
this.dst = dst;
this.cost = cost;
}
public Vertex getDst() {
return dst;
}
@Override
public int compareTo(Edge o)
{
if(o == null)
return -1;
if(this == null)
return -1;
if( this.dst.toString().compareTo( ((Edge)o).dst.toString() ) == 0 )
return 0;
else if ( this.dst.toString().compareTo( ((Edge)o).dst.toString() ) < 0 )
return 1;
else
return -1;
}
public String toString() {
String theEdgeS = this.dst.toString()+" "+Double.toString(cost);
return theEdgeS;
}
}
不确定为什么 Eclipse 环境处理代码的方式与命令行环境不同。类(class)是类(class)的一项正在进行的工作。我只是想解决看起来像是 Eclipse 错误的问题。
最佳答案
看起来 eclipse 有一个旧版本的 Graph.class
。您应该尝试清理项目:参见问题 Function of Project > Clean in Eclipse
关于java.lang.NoSuchMethodError : Graph: method <init>()V not found 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16704957/
这段代码无法编译: for(vector::iterator it = shapes.end(); it >= shapes.begin(); --it){ *it.update(1,1);
我一直在研究 Common Lisp 对象协议(protocol) (CLOS),我遇到了一个疑问。 有人知道 CLOS 中的“标准方法组合”和“简单方法组合”是什么意思吗? 在“简单方法组合”中,“
在Rust上对值调用方法之间是否有任何区别,如下所示: struct A { e: u32 } impl A { fn show(&self) { println!("{}",
我在一些 StackOverflow 答案中看到了术语抽象方法、具体方法和默认方法的“不同”定义。 Java 语言规范给出的真正定义是什么?请在您的答案中包含相关的支持 JLS 引用资料。 最佳答案
如果method = "post",如何使rest[method]扩展为rest.post(uri, body).then(. .? function proxyUrl() { return
这个问题在这里已经有了答案: Method cannot be translated into a store expression (1 个回答) 关闭 9 年前。 我有一个问题。我在 Visua
它们各自的优缺点是什么? 接口(interface)方法 虚方法 抽象方法 什么时候应该选择什么?做出这一决定时应牢记哪些要点? 最佳答案 虚拟和抽象几乎是一样的。虚方法在基类中有一个可以选择被覆盖的
我在 Meteor.js 上的那段代码出错: 客户端 : Meteor.call("logUser", function(myvar){ console.log("le c
运行代码时出现以下错误 Line: 18 illegal start of expression Line: 18 ';' expected 这意味着第 18 行中有代码写得不正确(public bo
如果可能的话,如何从另一个方法的返回中调用一个方法? 例如…… class Example { public static void main(String[] args) {
当遍历指针的 vector (或其他容器)时,使用以下优势和/或优势之间是否有任何区别: for (it = v.begin(); it != v.end(); ++it) { (*it)->
在从带有参数的 void 方法打印值或将值返回给方法调用者并在方法调用者中打印它之间,哪个被认为是更好的做法(如果有的话)?比如第一个代码摘录是前者,第二个代码摘录是后者: public static
考虑这个例子https://codesandbox.io/s/1yvp4zz5x7?module=%2Fsrc%2FApp.vue Greet1 Greet2
晚上好, 我刚开始使用 Microsoft.Contracts(最新版本)并将其插入示例界面之上,现在它看起来像这样: namespace iRMA2.Core.Interfaces { us
我是 Laravel 4 的新手,并试图弄清楚为什么我收到一个错误,说 Method [show] 不存在。 我没有名为“show”的方法,只能想象这是一个内部的 Laravel 方法,但我不知道如何
有人可以向我解释一下当我们进行下一次返回时“或”(||) 是什么意思吗? 我的意思是这行: 返回封面(值,金额 - 值 [索引],索引 + 1)||覆盖(值、金额、索引 + 1); public st
这个问题已经有答案了: Why doesn't the post increment operator work on a method that returns an int? (11 个回答) 已
我很难理解 jQuery 的 $.method() 和 $(selector).method 之间的区别。 $.method() 实际适用于 DOM 中的哪些元素?如果有人能帮助解释这两种说法之间的区
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 5 年前。 Improve t
////////////////////////////////////////////////////////////////////////////// // 3 construct
我是一名优秀的程序员,十分优秀!