-3

我无法为 getLines 方法打印,我在这里做错了吗?当我运行程序时它没有给我任何错误,但是当我尝试打印 getlines 方法时,它给了我错误。

当我尝试打印 getlines 方法时,它给了我这个错误。线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 在 dijkstra.Fileprocess.getLines(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;
        }


}
4

1 回答 1

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
    }
}
于 2013-02-25T14:16:01.833 回答