代码之家  ›  专栏  ›  技术社区  ›  BUY

金字塔最小和路径2d arraylist indexoutbounds

  •  0
  • BUY  · 技术社区  · 7 年前

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

    我的文本文件示例:

    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 回复  |  直到 7 年前
        1
  •  2
  •   XtremeBaumer    7 年前

    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访问的