在这个程序中找到java.lang.ClassCastException,有人能帮我吗?
我正在为一个能够根据ID绘制城市地图的图形编写一个代码,当我搜索一个顶点的后续对象时,会弹出这个错误。有人能帮我吗 确切的问题发生在DataStructure类中,但我决定,如果您看到我没有看到的错误,我将发布整个代码 这就是我得到的错误: 提前谢谢大家 这是我的代码:在这个程序中找到java.lang.ClassCastException,有人能帮我吗?,java,classcastexception,Java,Classcastexception,我正在为一个能够根据ID绘制城市地图的图形编写一个代码,当我搜索一个顶点的后续对象时,会弹出这个错误。有人能帮我吗 确切的问题发生在DataStructure类中,但我决定,如果您看到我没有看到的错误,我将发布整个代码 这就是我得到的错误: 提前谢谢大家 这是我的代码: import java.util.*; public class Main { public static void main() { DataRead d = new DataRead();
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;
}
}
import java.util.*;
公共班机{
公共静态void main(){
DataRead d=新的DataRead();
HashMap vertex=d.readVertex();
System.out.println(vertex.size());
ArrayList Edge=d.ReadEdge();
System.out.println(edges.size());
数据结构e=新的数据结构(顶点、边);
System.out.println(e.adjGraph.size());
System.out.println(vertex.get(new Long(287291920)).id);
长l=新长(287291920);
长l2=新长(1397149003);
arraylisttest1=e.getl(l);
System.out.println(test1.size());
for(长i:test1){
系统输出打印LN(i);
}
System.out.println(“距离:+e.getDistance(l,l2));
}
}
公共类节点{
长id;
公共节点(长id){
this.id=id;
}
}
公共类三元组{
私人决赛第一名;
私人决赛b秒;
私人终审法院;
公共三元组(a第一、b第二、c区){
this.first=first;
这个秒=秒;
this.dist=dist;
}
公共a getFirst(){
先返回;
}
公共b getSecond(){
返回第二;
}
公共c getDistance(){
返回距离;
}
}
导入java.util.*;
导入java.io.*;
导入java.lang.Object;
公共类数据读取{
公共HashMap readVertex(){
BufferedReader br=null;
HashMap顶点=新HashMap();
试一试{
br=新的BufferedReader(新的文件读取器(“texts.txt”);
弦线;
整数计数=0;
而((line=br.readLine())!=null){
字符串[]arr=line.split(“”);
Long id=Long.parseLong(arr[0]);
节点v=新节点(id);
顶点放置(v.id,v);
}
br.close();
}捕获(IOE异常){
System.out.println(“找不到您的文件”);
}
返回顶点;
}
公共ArrayList ReadEdge(){
ArrayList Edge=新的ArrayList();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(“Arcos.txt”);
弦线;
而((line=br.readLine())!=null){
字符串[]arr=line.split(“”);
Long origin=Long.parseLong(arr[0]);
Long destination=Long.parseLong(arr[1]);
Double dist=Double.parseDouble(arr[2]);
三元组t=新三元组(起点、终点、距离);
添加(t);
}
br.close();
}捕获(IOE异常){
System.out.println(“找不到您的文件”);
}
返回边;
}
}
导入java.util.*;
公共类数据结构{
ArrayList adjGraph=新的ArrayList();
公共数据结构(HashMap顶点、ArrayList边){
用于(三重t:边){
三元组三元组=新三元组(t.getFirst(),t.getSecond(),t.getDistance());
adjGraph.add(三元组);
}
}
公共阵列列表(长视频){
ArrayList继承者=新的ArrayList();
对于(int i=0;i
问题在于代码中存在一些未经检查的转换:
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
javac-Xlint:unchecked*.java
java:41:警告:[未选中]未选中作为原始类型三元组成员对三元组(a、b、c)的调用
三元组t=新三元组(起点、终点、距离);
^
其中a、b、c是类型变量:
在类三元组中声明的扩展对象
b扩展类三元组中声明的对象
c扩展类三元组中声明的对象
java:41:警告:[未选中]未选中的转换
三元组t=新三元组(起点、终点、距离);
^
必需:三元组
发现:三胞胎
java:9:警告:[未选中]未选中调用Triplet(a、b、c)作为原始类型Triplet的成员
三元组三元组=新三元组(t.getFirst(),t.getSecond(),t.getDistance());
^
其中a、b、c是类型变量:
在类三元组中声明的扩展对象
b扩展类三元组中声明的对象
c扩展类三元组中声明的对象
java:9:警告:[未选中]未选中的转换
三元组三元组=新三元组(t.getFirst(),t.getSecond(),t.getDistance());
^
必需:三元组
发现:三胞胎
注意:Main.java使用或覆盖不推荐使用的API。
注意:有关详细信息,请使用-Xlint:deprecation重新编译。
4警告
这些都会导致“堆污染”,最终导致泛型类的某个字段中出现错误类型的值
我想我可以看到实际的公关
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