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

时间:2018-06-04 10:56:30

标签: 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)

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

如果有人可以帮我解决这个错误,我会很高兴他/她。

1 个答案:

答案 0 :(得分:2)

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<asp:Repeater ID="rptProjectSubFolders" runat="server"> <ItemTemplate> <asp:HiddenField runat="server" ID="fileID" ClientIDMode="Static" Value='<%# Container.DataItem("FileID") %>' /> <div style="width: 15%;text-align: right;"> <asp:LinkButton OnCommand="btnEditImage_Click" CssClass="link-button" Text="Edit Image" runat="server" ID="btn_ImageEdit" CommandArgument='<%# Container.DataItem("FileID")%>' /> </div> </ItemTemplate> </asp:Repeater> 都是基于0的。这意味着第一个元素实际上是0,而不是1