简体   繁体   English

java 程序的索引越界错误

[英]Index out of bounds error for java program

I'm getting this error and I need help.我收到此错误,我需要帮助。 Code reads in a.txt file and gives an output.代码读取 .txt 文件并给出 output。 I believe it should be a simple fix but am unable to find a solution.我相信这应该是一个简单的修复,但我无法找到解决方案。 The first token represents the name of a room in the dungeon.第一个标记代表地牢中一个房间的名称。 After that, each pair of tokens represents a connected room and the steps it will take to travel to that room.之后,每对令牌代表一个连接的房间以及前往该房间所需的步骤。 (A room can be connected to up to 32 other rooms). (一个房间最多可以连接 32 个其他房间)。 If a room doesn't have an entry in the input file, this means that it does not have any way out.如果一个房间在输入文件中没有条目,这意味着它没有任何出路。 Every dungeon entrance is named A. When there are multiple connected rooms, they should be explored in alphabetic order.每个地下城入口都命名为A。当有多个相连的房间时,应按字母顺序探索。 The room labeled X contains a heart container.标有 X 的房间包含一个心脏容器。 Not all rooms are connected.并非所有房间都相连。 Input:输入:

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: Number of Potions Needed: 0 Distance to Chest: 20 Path to Chest: AGX Output:需要的药水数量:0 到箱子的距离:20 到箱子的路径:AGX

Code:代码:

 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);
            }
        }
    }

The index variable i is incremented by 2 thus the last if block tries to access ahead of the last index.索引变量i增加2 ,因此最后一个if块尝试在最后一个索引之前访问。 You need to add some guard code to break the loop before the last if block.您需要在最后一个if块之前添加一些保护代码来break循环。

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

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