gpt4 book ai didi

java - 越界但不应该

转载 作者:行者123 更新时间:2023-12-01 23:23:47 25 4
gpt4 key购买 nike

public class Ctrl {

Graph g = new Graph();
Edge[] edges = new Edge[g.getM()];
int[] verteY = new int[g.getM()];
int[] verteX = new int[g.getM()];
int[] vCost = new int[g.getM()];
int contor=0;

public void add(int x,int y,int c) {
System.out.println("contor:" + this.contor);
System.out.println("M:" + g.getM());
verteX[this.contor] = x;
verteY[this.contor] = y;
vCost[this.contor] = c;
this.contor = this.contor + 1;
}

输出为

contor:0
M:5

为什么我会得到 java.lang.ArrayIndexOutOfBoundsException: 0 那么?

最佳答案

新初始化的GraphgetM()很可能返回零,使得所有四个数组的大小都为零。

如果 g.getM() 稍后发生更改,数组不会自动调整大小。

我的建议是使用ArrayList而不是原始数组。这将使附加到它们变得容易。

关于java - 越界但不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com