我正在为图形编写代码,该图形能够基于id绘制城市,当我搜索顶点的后继者时,会弹出此错误。有人可以帮我吗?
确切的问题发生在DataStructure类中,但是我决定我将发布整个代码,即使您看到的是我没有看到的错误。
这是我得到的错误:
java.lang.ClassCastException: class java.lang.Long cannot be cast to class Node (java.lang.Long is in module java.base of loader 'bootstrap'; Node is in unnamed module of loader java.net.URLClassLoader @3df5c641) at DataStructure.getSuccessors(DataStructure.java:19) at Main.main(Main.java:18)
提前感谢大家!
这是我的代码:
import java.util.*;
public class Main {
public static void main() {
DataRead d = new DataRead();
HashMap<Long, Node> vertex = d.readVertex();
System.out.println(vertex.size());
ArrayList<Triplet<Long, Long, Double>> edges = d.readEdges();
System.out.println(edges.size());
DataStructure e = new DataStructure(vertex, edges);
System.out.println(e.adjGraph.size());
System.out.println(vertex.get(new Long(287291920)).id);
Long l = new Long(287291920);
Long l2 = new Long(1397149003);
ArrayList<Long> test1 = e.getSuccessors(l);
System.out.println(test1.size());
for(Long i : test1) {
System.out.println(i);
}
System.out.println("Distance: " + e.getDistance(l, l2));
}
}
public class Node {
Long id;
public Node(Long id) {
this.id = id;
}
}
public class Triplet<a, b, c> {
private final a first;
private final b second;
private final c dist;
public Triplet(a first, b second, c dist) {
this.first = first;
this.second = second;
this.dist = dist;
}
public a getFirst() {
return first;
}
public b getSecond() {
return second;
}
public c getDistance() {
return dist;
}
}
import java.util.*;
import java.io.*;
import java.lang.Object;
public class DataRead {
public HashMap<Long, Node> readVertex() {
BufferedReader br = null;
HashMap<Long, Node> vertex = new HashMap<>();
try {
br = new BufferedReader(new FileReader("Vertices.txt"));
String line;
int count = 0;
while((line = br.readLine()) != null) {
String[] arr = line.split(" ");
Long id = Long.parseLong(arr[0]);
Node v = new Node(id);
vertex.put(v.id, v);
}
br.close();
} catch (IOException e) {
System.out.println("Your file couldn't be found");
}
return vertex;
}
public ArrayList<Triplet<Long, Long, Double>> readEdges() {
ArrayList<Triplet<Long, Long, Double>> edges = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader("Arcos.txt"));
String line;
while((line = br.readLine()) != null) {
String[] arr = line.split(" ");
Long origin = Long.parseLong(arr[0]);
Long destination = Long.parseLong(arr[1]);
Double dist = Double.parseDouble(arr[2]);
Triplet<Long, Long, Double> t = new Triplet(origin, destination, dist);
edges.add(t);
}
br.close();
} catch (IOException e) {
System.out.println("Your file couldn't be found");
}
return edges;
}
}
import java.util.*;
public class DataStructure {
ArrayList<Triplet<Node, Node, Double>> adjGraph = new ArrayList<>();
public DataStructure (HashMap<Long, Node> vertex, ArrayList<Triplet<Long, Long, Double>> edges) {
for(Triplet<Long, Long, Double> t : edges) {
Triplet<Node, Node, Double> triplet = new Triplet(t.getFirst(), t.getSecond(), t.getDistance());
adjGraph.add(triplet);
}
}
public ArrayList<Long> getSuccessors (Long Vid) {
ArrayList<Long> successors = new ArrayList<>();
for(int i = 0; i < adjGraph.size(); i++) {
Triplet<Node, Node, Double> t = adjGraph.get(i);
if(Vid == adjGraph.get(i).getFirst().id) {
successors.add(adjGraph.get(i).getSecond().id);
}
}
return successors;
}
public Double getDistance(Long sourceID, Long destinationID){
for(int i=0; i < adjGraph.size(); i++){
if((sourceID == adjGraph.get(i).getFirst().id) && (destinationID == adjGraph.get(i).getSecond().id)){
return adjGraph.get(i).getDistance();
}
}
return -1.0;
}
}
答案 0 :(得分:0)
问题是您的代码中有一些未经检查的转换:
javac -Xlint:unchecked *.java
DataRead.java:41: warning: [unchecked] unchecked call to Triplet(a,b,c) as a member of the raw type Triplet
Triplet<Long, Long, Double> t = new Triplet(origin, destination, dist);
^
where a,b,c are type-variables:
a extends Object declared in class Triplet
b extends Object declared in class Triplet
c extends Object declared in class Triplet
DataRead.java:41: warning: [unchecked] unchecked conversion
Triplet<Long, Long, Double> t = new Triplet(origin, destination, dist);
^
required: Triplet<Long,Long,Double>
found: Triplet
DataStructure.java:9: warning: [unchecked] unchecked call to Triplet(a,b,c) as a member of the raw type Triplet
Triplet<Node, Node, Double> triplet = new Triplet(t.getFirst(), t.getSecond(), t.getDistance());
^
where a,b,c are type-variables:
a extends Object declared in class Triplet
b extends Object declared in class Triplet
c extends Object declared in class Triplet
DataStructure.java:9: warning: [unchecked] unchecked conversion
Triplet<Node, Node, Double> triplet = new Triplet(t.getFirst(), t.getSecond(), t.getDistance());
^
required: Triplet<Node,Node,Double>
found: Triplet
Note: Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 warnings
这些正在导致“堆污染”,最终您在一个通用类之一的字段之一中输入错误类型的值。
我认为我可以看到实际的问题,但是因为这是您的作业,我将留给您阅读上面的编译警告,并弄清楚您做错了什么。
(提示:您使用Triplet
的情况不一致...)
这里的教训是,在使用泛型类型时,您不应忽略有关未经检查的转换等的编译器警告。编译器基本上是在说“我不知道这是否正确”。而且,如果代码不正确,您将在运行时在意外的地方得到类强制转换异常。