我无法打印getLines方法,我在这里做错了什么?它不会给我任何错误,当我运行该程序,但是当我尝试打印getlines方法时,它给我错误。无法打印阵列列表
当我尝试打印getlines方法时,它给了我这个错误。 异常在线程 “主” java.lang.ArrayIndexOutOfBoundsException:在dijkstra.Fileprocess.getLines 1(Fileprocess.java:37)在dijkstra.Fileprocess.main(Fileprocess.java:70)
public class Fileprocess {
public static Scanner Reader(String FileName){
//Pass a File Name to this method, then will return Scanner for reading data from that file
try {
return new Scanner(new File(FileName));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
System.exit(1);
return null;
}
}
static ArrayList<Edge> getLines(ArrayList<Vertex> PointCollection) {
Scanner Input = Reader(Vertex.graph);
ArrayList<Edge> result = new ArrayList<Edge>();
while(Input.hasNext()){
String line = Input.nextLine();
String[] arr = line.split(" ");
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
}
Input.close();
return result;
}
static ArrayList<Vertex> getPoints() {
Scanner Input = Reader(Vertex.airports);
ArrayList<Vertex> result = new ArrayList<Vertex>();
while(Input.hasNext()){
String line = Input.nextLine();
result.add(new Vertex(line));
}
Input.close();
return result;
}
static Vertex getPointbyIndex(ArrayList<Vertex>PointCollection, int Index){
for(Vertex p:PointCollection){
if(p.getIndex() == Index){
return p;
}
}
return null;
}
public static void main(String[] args){
System.out.println(getPoints());
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;
}
}
问题是什么?如果您不告诉我们问题是什么,我们该如何帮忙? – Doorknob 2013-02-25 14:10:57
当我尝试打印getlines方法时,它给了我这个错误。异常在线程 “主要” java.lang.ArrayIndexOutOfBoundsException:1 \t在dijkstra.Fileprocess.getLines(Fileprocess.java:37) \t在dijkstra.Fileprocess.main(Fileprocess.java:70) – mon999 2013-02-25 14:12:28
好吧,那么也许你应该把它在问题! – Doorknob 2013-02-25 14:13:10