我無法打印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