【问题标题】:Index out of bounds error for java programjava程序的索引越界错误
【发布时间】:2022-01-13 22:39:02
【问题描述】:

我遇到了这个错误,我需要帮助。代码读入 .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);
            }
        }
    }

【问题讨论】:

  • 异常发生在哪一行代码?
  • if (!uniqueVertices.contains(vertex[i].charAt(0))) {// 如果唯一顶点映射中不存在顶点,则添加它
  • 在最后一个 if 块中用 i-1 替换 i 时,我得到的最终输出为 [A, B, 2, 1, 7, C, E, 8, D, 5, 4、9、3、F、G、H、I、J、K、L、M、6、N]

标签: java exception indexing error-handling string-length


【解决方案1】:

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

【讨论】:

  • 你有我如何实现它的例子吗?
  • if(i >= vertex.length) { // 打印顶点然后中断;
  • 添加这个我得到 [A, C, B, D, E, X] 和 2147483647 的距离。正确答案应该是 [A, B, C, D, E, X]距离为 68。代码中是否有错字?
猜你喜欢
  • 2013-10-13
  • 1970-01-01
  • 2014-06-06
  • 2015-03-22
  • 2022-08-24
  • 2015-01-10
  • 2014-01-27
相关资源
最近更新 更多