Java 无法打印arraylist
我无法打印getLines方法,我在这里做错了什么吗?当我运行程序时,它不会给我任何错误,但是当我试图打印getlines方法时,它会给我错误Java 无法打印arraylist,java,Java,我无法打印getLines方法,我在这里做错了什么吗?当我运行程序时,它不会给我任何错误,但是当我试图打印getlines方法时,它会给我错误 当我试图打印getlines方法时,它会给我带来这种错误。 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:dijkstra.Fileprocess.getLines(Fileprocess.java:37)中的异常位于dijkstra.Fileprocess.main(Fileprocess.j
当我试图打印getlines方法时,它会给我带来这种错误。 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:dijkstra.Fileprocess.getLines(Fileprocess.java:37)中的异常位于dijkstra.Fileprocess.main(Fileprocess.java:70)
可能您的文件包含空行或不兼容行 您可以使用以下方法修复错误:
String[] arr = line.split(" ");
if (arr.length > 3) {
result.add(new Edge(Integer.parseInt(arr[0]), //index
getPointbyIndex(PointCollection,Integer.parseInt(arr[1])), //start
getPointbyIndex(PointCollection,Integer.parseInt(arr[2])), //end
Integer.parseInt(arr[3]))); //cost
}
}
有什么问题?如果您不告诉我们问题出在哪里,我们该如何帮助呢?当我尝试打印getlines方法时,它会给我带来这些错误。线程“main”java.lang.ArrayIndexOutOfBoundsException:1在dijkstra.Fileprocess.getLines(Fileprocess.java:37)在dijkstra.Fileprocess.main(Fileprocess.java:70)那么也许你应该把它放在问题中!您需要检查分割数组的长度是否正确。if(arr.length==3)等哪一行是第37行?文件中没有任何空行,但在我输入代码后,它现在不会给我一个错误,但是我可以问if(arr.length>3)做什么吗???你之所以会出错,是因为你试图访问一个没有该索引的数组中的索引。在这里,只有当这个索引存在时,你才尝试访问数组。现在没有给我一个错误,但我不确定如何打印文件,我可以使用System.out.println(null);?您不需要更改println命令。您是否看到任何错误?您使用System.out.println(getLines(null));?
1 1 2 2
2 1 3 1
3 1 6 3
4 1 7 3
5 2 1 2
6 2 3 1
7 2 4 1
8 2 5 2
9 2 6 2
10 3 1 1
11 3 2 1
12 3 4 1
class Edge {
public Vertex start;
public Vertex end;
public double cost;
public int Index;
// public final Vertex target;
// public final int weight;
public Edge(double cost, Vertex end, Vertex start, int Index){
this.start = start;
this.end = end;
this.cost = cost;
this.Index = Index;
}
public String toString(){
String result = "";
if(this.start != null && this.end != null){
result = this.Index +","+this.start.Index+","+this.end.Index +","+this.cost;
}
return result;
}
}
String[] arr = line.split(" ");
if (arr.length > 3) {
result.add(new Edge(Integer.parseInt(arr[0]), //index
getPointbyIndex(PointCollection,Integer.parseInt(arr[1])), //start
getPointbyIndex(PointCollection,Integer.parseInt(arr[2])), //end
Integer.parseInt(arr[3]))); //cost
}
}