java 程序的索引越界错误

Index out of bounds error for java program

我遇到了这个错误,我需要帮助。代码读取 .txt 文件并给出输出。我相信这应该是一个简单的修复,但我无法找到解决方案。第一个标记代表名称 地牢中的一个房间。之后,每对代币代表一个相连的房间和它要进行的步骤 带去那个房间。 (一个房间最多可以连接到其他 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

输出: 所需药水数量: 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 添加一些保护代码。