java - 金字塔最小和路径2D ArrayList IndexOutOfBounds

标签 java arraylist indexoutofboundsexception

我正在尝试计算金字塔的最小自下而上路径总和。我正在从文本文件读取输入。

我的文本文件示例:

5
6
6 5
3 5 8
3 9 2 1
4 7 9 2 7

第一行告知程序有关金字塔的大小,其他行正在组装金字塔。 (在此示例中,大小为 5)。

这是我的主要方法:

public static void main(String[] args) throws FileNotFoundException {

    if(args.length > 0) {   
        File file1 = new File(args[0]);
        Scanner scanner1 = new Scanner(file1); // The Scanner that is going to read file1.
        int pyramidSize = scanner1.nextInt();
        scanner1.next();
        System.out.println("Pyramid Size = " + pyramidSize);
        ArrayList<ArrayList <Integer>> solution1List = new ArrayList<ArrayList <Integer>>(pyramidSize);
        // Reads all integers to an array list.

        while(scanner1.hasNext()) {
            int row = 1;

            if(scanner1.hasNextInt()) {    
                int temporaryForSolution1Integer = scanner1.nextInt();
                solution1List.get(row).add(temporaryForSolution1Integer);
            }
            else {
                scanner1.next();
                row++;
            }


            scanner1.close(); // Closed the scanner in order to prevent resource leak.
        }

         System.out.println("The minimum sum path is = " + minimumSumPath(solution1List));

这是我用来计算最小路径的方法:

public static int minimumSumPath(ArrayList<ArrayList<Integer>> triangle) {

    if (triangle.size() == 0)
        return 0;
    int size = triangle.size();
    int min = Integer.MAX_VALUE;

    int[] sum = new int[size];
    sum[0] = triangle.get(0).get(0);
    for(int current = 1; current <= size - 1; current++){
        int next_size = triangle.get(current).size();
        // it has to start with the end of the array
        // because the further one need the clean sum 
        // value than the newer one.
        for(int next = next_size - 1; next >= 0; next--) {
            if (next == 0) {
                sum[0] = sum[0] + triangle.get(current).get(next);
            } else if (next == (next_size - 1)) { // Reaches to the rightmost element of that iteration
                sum[next] = sum[next-1] + triangle.get(current).get(next);
            } else { // Provides sum[next] to be the minimal sum that can come there
                sum[next] = Math.min(sum[next-1], sum[next]) + triangle.get(current).get(next);
            }
        }
    }

    for(int i = 0; i < size; i++){
        if(sum[i] < min){
           min = sum[i];
        }
    }
    return min;
}

目前我遇到了如下错误:

PS C:\Users\berku\Desktop\BERKSOL> javac .\berkSol.java
PS C:\Users\berku\Desktop\BERKSOL> java berkSol input1.txt input2.txt
Pyramid Size = 5
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at berkSol.main(berkSol.java:23)

我认为我的计算方法是正确的。

如果有人可以帮助我解决这个错误,我将非常高兴。

最佳答案

solution1List.get(row).add(temporaryForSolution1Integer);该索引处没有元素。

您创建一个二维列表: ArrayList<ArrayList <Integer>> solution1List = new ArrayList<ArrayList <Integer>>(pyramidSize); ,但您从未添加实际的 List元素。

在你可以做solution1List.get(0);之前你必须做solution1List.add(new ArrayList<Integer>());

<小时/>

请注意Lists以及 Arrays是从 0 开始的。这意味着第一个元素实际上是用 0 而不是 1 访问的

关于java - 金字塔最小和路径2D ArrayList IndexOutOfBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50678987/

相关文章:

java - 使用 JOOX 过滤来自 REST 端点的 xml 数据

java - Docker tomcat找不到应用程序日志

java - 为什么 java.util.ArrayList 包中的数据数组是私有(private)的?

java - 用一行二维 ArrayList 填充一维 Arraylist

JAVA:如何使用扫描仪向字符串数组输入未定义数量的值?

java - 如何按每个字符串中元音的数量对字符串数组进行升序排序?

java - 错误的 boolean 映射 Hibernate (ArrayIndexOutOfBoundsException)

java - 如何解析从客户端收到的xml结果

java - 错误: validation failed for objects with date

java - 使用 split() 并检查第二个元素