【问题标题】:Dynamic programming ArrayIndexOutOfBoundException动态编程 ArrayIndexOutOfBoundException
【发布时间】:2011-03-11 17:57:25
【问题描述】:

我遇到了这个奇怪的异常,我真的不明白为什么..我尝试调试并发现它在运行时出错:

opt[i][j] = Double.POSITIVE_INFINITY; 

当 i == 0 和 j == 1 时,这不应该发生,因为在这种情况下 opt 是一个 9x6 矩阵。

这是我的代码:

public class Versie3 {

    private int desCap;
    private int currentCap;
    private int maxCap;
    private int timeSlot;
    private static ArrayList<Double> prices;
    private double[][] opt = new double[timeSlot + 1][maxCap + 1];

    public Versie3() throws FileNotFoundException {

    }

    public void readInput(String s) throws FileNotFoundException 
    {
        FileReader fr = new FileReader(s);
        Scanner sc = new Scanner(fr);

        timeSlot = sc.nextInt();
        maxCap = sc.nextInt();
        currentCap = sc.nextInt();
        desCap = sc.nextInt();
        prices = new ArrayList<Double>(timeSlot);

        while (sc.hasNextDouble()) {
            prices.add(sc.nextDouble());

        }
    }

    public double calculateOptimal() 
    {
        for (int i = 0; i <= timeSlot; i++) 
        {
            for (int j = 0; j <= maxCap; j++) 
            {
                if (i == 0) 
                {
                    if (j != desCap) 
                    {

                        opt[i][j] = Double.POSITIVE_INFINITY; // <--here it goes Wrong!
                    } 
                    else 
                    {
                        opt[i][j] = 0;
                    }
                } 
                else if (j == 0) 
                {
                    opt[i][j] = Math.min(opt[i - 1][j],
                            opt[i - 1][j + 1]
                                    - prices.get(i-1));
                } 
                else if (j == maxCap) 
                {
                    opt[i][j] = Math.min(opt[i - 1][j],
                            opt[i - 1][j - 1]
                                    + prices.get(i-1));
                } 
                else 
                {
                    opt[i][j] = Math.min(Math.min(opt[i - 1][j],
                    opt[i - 1][j - 1]
                    + prices.get(i - 1)),opt[i - 1][j + 1]- prices.get(i-1));
                }
            }
        }
        return opt[timeSlot][currentCap];
    }

    public static void main(String[] args) throws FileNotFoundException {
        Versie3 v3 = new Versie3();
        v3.readInput("input.txt");
        System.out.println("prices: " + prices.toString());
        System.out.println("timeSlot: " + v3.timeSlot);
        System.out.println("maxCap: " + v3.maxCap);
        System.out.println("currentCap: " + v3.currentCap);
        System.out.println("desCap: " + v3.desCap);
        //System.out.println("minimum cost: "+v3.calculateOptimal());
        System.out.println(v3.prices.size());

    }

}

这是我正在阅读的输入文件:

8 5 2 5
2.2 3 5 6.5 5 5 3 1.8

在这种情况下:

timeSlot = 8
maxCap = 5
currentCap = 2 
desCap = 5

第二行显示每个时间段的价格。总共 8 个。

感谢您的帮助。

【问题讨论】:

  • +1 个结构合理的问题,包含所有必要的信息。

标签: java dynamic-programming


【解决方案1】:

opt 在构造时初始化之前 timeSlotmaxcap 已设置。

所以你创建一个数组

private double[][] opt = new double[0 + 1][0 + 1];

您必须在用户输入值后在readInput 方法中创建数组。

【讨论】:

    【解决方案2】:

    您正在使用maxCaptimeSlot 创建数组,而它们仍具有0 的默认值。 readInput() 还没有被调用,你怎么知道数组的大小?

    在读入maxCaptimeSlot 后创建数组。

    【讨论】:

      【解决方案3】:

      在确定 maxCap 等于什么之前初始化 opt 数组。

      【讨论】:

        【解决方案4】:

        当您创建Versie3 类的对象时,maxCaptimeSlot 采用它们的默认值 0,并创建数组 opt,其大小为 1 x 1

        在此之后,您去读取重写maxCaptimeSlot 值的文件,但数组大小保持不变

        要解决此问题,请在读取维度后在函数 readFile 中为数组分配内存。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-27
          相关资源
          最近更新 更多