在这个程序上找到 java.lang.ClassCastException,有人可以帮助我吗?
Finding java.lang.ClassCastException on this program, can someone help me?
我正在为一个图表编写代码,该图表能够根据 ID 映射城市,但在搜索顶点的后继者时弹出此错误。有人可以帮我吗?
DataStructure class 中出现了确切的问题,但我决定 post 整个代码,如果您看到我没有看到的错误。
这是我遇到的错误:
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;
}
}
问题是您的代码中有一些未经检查的转换:
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
这些导致 "heap pollution" 您最终在您的通用 class 之一的字段之一中得到错误类型的值。
我想我可以看出实际问题是什么,但是因为这是你的作业我会留给你阅读上面的编译警告并弄清楚你做了什么错了。
(提示:您使用的 Triplet
不一致...)
这里的教训是,当您使用泛型类型时,您不应该忽略有关未经检查的转换等的编译器警告。编译器基本上是说 "I don't know if this is correct"。如果代码不正确,您将在运行时在意想不到的地方得到一个 class 转换异常。
我正在为一个图表编写代码,该图表能够根据 ID 映射城市,但在搜索顶点的后继者时弹出此错误。有人可以帮我吗?
DataStructure class 中出现了确切的问题,但我决定 post 整个代码,如果您看到我没有看到的错误。
这是我遇到的错误:
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;
}
}
问题是您的代码中有一些未经检查的转换:
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
这些导致 "heap pollution" 您最终在您的通用 class 之一的字段之一中得到错误类型的值。
我想我可以看出实际问题是什么,但是因为这是你的作业我会留给你阅读上面的编译警告并弄清楚你做了什么错了。
(提示:您使用的 Triplet
不一致...)
这里的教训是,当您使用泛型类型时,您不应该忽略有关未经检查的转换等的编译器警告。编译器基本上是说 "I don't know if this is correct"。如果代码不正确,您将在运行时在意想不到的地方得到一个 class 转换异常。