繁体   English   中英

java 程序的索引越界错误

[英]Index out of bounds error for java program

我收到此错误,我需要帮助。 代码读取 .txt 文件并给出 output。 我相信这应该是一个简单的修复,但我无法找到解决方案。 第一个标记代表地牢中一个房间的名称。 之后,每对令牌代表一个连接的房间以及前往该房间所需的步骤。 (一个房间最多可以连接 32 个其他房间)。 如果一个房间在输入文件中没有条目,这意味着它没有任何出路。 每个地下城入口都命名为A。当有多个相连的房间时,应按字母顺序探索。 标有 X 的房间包含一个心脏容器。 并非所有房间都相连。 输入:

A B 25 G 11 I 7
B C 10 E 12
C E 1 H 25 L 88
D A 51 J 2 N 48
E A 99 B 23 K 3
F C 23 D 3 E 75
G A 12 D 29 X 9 
H G 1 C 2 M 10
I E 7 B 2 A 9
J I 12 H 11 G 13 F 27 D 85
K A 12 B 13
L E 70 G 50 J 20 K 1 B 4
M L 10 A 11 F 91 H 67 N 92
N M 1

Output:需要的药水数量:0 到箱子的距离:20 到箱子的路径:AGX

代码:

 class GraphEdge {
        private char from;
        private char to;
        private int weight;
    public GraphEdge(char from, char to, int weight) {
        this.from = from;
        this.to = to;
        this.weight = weight;
    }

    public class Graph {
        public static void main(String[] args) throws FileNotFoundException {
            DFS();}
    private static void DFS() 
        char source = 'A';// source vertex
        char destination = 'X';// destination vertex
        ArrayList<GraphEdge> edges = new ArrayList<GraphEdge>();// list to hold all edges
        HashSet<Character> uniqueVertices = new HashSet<Character>();// all unique vertices in map
        ArrayList<Character> allVertices = new ArrayList<Character>();// list to hold all vertices
        // System.out.println("Input File:");
        while (obj.hasNextLine()) {// while input file has lines left
            String line = obj.nextLine();
            System.out.println(line);
            String vertex[] = line.split(" ");// split the line into source vertex, destination vertex and the weight
            int i = 1;
        while (i < vertex.length) {
            GraphEdge edge = new GraphEdge(vertex[0].charAt(0), vertex[i].charAt(0),Integer.parseInt(vertex[i + 1]));
            edges.add(edge);
            i = i + 2;
            System.out.println(edge);
            System.out.println(vertex[0].charAt(0));
            System.out.println(vertex[1].charAt(0));
            //System.out.println(vertex[i].charAt(0));
            if (!uniqueVertices.contains(vertex[0].charAt(0))) {// if vertex not present in unique vertices map, add it
                // in map and arraylist
                uniqueVertices.add(vertex[0].charAt(0));
                allVertices.add(vertex[0].charAt(0));
                System.out.println(allVertices);
            }
            if (!uniqueVertices.contains(vertex[1].charAt(0))) {// if vertex not present in unique vertices map, add it
                // in map and arraylist
                uniqueVertices.add(vertex[1].charAt(0));
                allVertices.add(vertex[1].charAt(0));
                System.out.println(allVertices);
            }
            if (!uniqueVertices.contains(vertex[i].charAt(0))) {// if vertex not present in unique vertices map, add it
                // in map and arraylist
                uniqueVertices.add(vertex[i].charAt(0));
                allVertices.add(vertex[i].charAt(0));
                System.out.println(allVertices);
            }
        }
    }

索引变量i增加2 ,因此最后一个if块尝试在最后一个索引之前访问。 您需要在最后一个if块之前添加一些保护代码来break循环。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM