2013-02-25 40 views
-3

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


} 
+3

問題是什麼?如果您不告訴我們問題是什麼,我們該如何幫忙? – Doorknob 2013-02-25 14:10:57

+0

當我嘗試打印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

+1

好吧,那麼也許你應該把它在問題! – Doorknob 2013-02-25 14:13:10

回答

0

也許你的文件中包含一個空的或不兼容線?

您可以修復使用的錯誤:

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 
    } 
} 
+0

文件中沒有任何空行,但它沒有給我一個錯誤後,我把我的代碼,但現在我可以問一下if(arr.length> 3)做什麼? – mon999 2013-02-25 14:18:46

+0

由於您嘗試訪問沒有該索引的數組中的索引,您得到了該錯誤。在這裏,只有在索引存在的情況下才嘗試訪問數組。 – BobTheBuilder 2013-02-25 14:20:02

+0

以及現在不給我一個錯誤,但我不知道如何我可以打印出文件,我可以使用System.out.println(null);? – mon999 2013-02-25 14:28:58