java - 为什么我的垂直乘积求和程序出现 "index out of bounds"异常?

标签 java arrays algorithm error-handling

因此,我正在尝试构建一个程序来获取表示 20x20 矩阵的整数列表(特别是 400),并找到此列表中四个垂直连续整数的最大乘积。在这种情况下,索引 0、20、40 和 60 处的数字将是垂直连续的数字。由于某种原因,java 控制台抛出以下错误:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 418 out of bounds for length 400 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) at Main.getDigitsVertical(Main.java:101) at Main.productListVertical(Main.java:59) at Main.main(Main.java:10) exit status 1

这是我的代码:

import java.math.BigInteger;
import java.io.*; 
import java.util.*; 

class Main {
  public static void main(String[] args) {

    String data = "08022297381500400075040507785212507791084949994017811857608717409843694804566200814931735579142993714067538830034913366552709523046011426924685601325671370236912231167151676389419236542240402866331380244732609903450244753353783684203517125032988128642367102638406759547066183864706726206802621220956394396308409166499421245558056673992697177878968314883489637221362309750076442045351400613397343133957817532822753167159403800462161409535692163905429635314755588824001754243629855786560048357189070544443744602158515417581980816805944769287392138652177704895540045208839735991607975732162626793327986688366887576220720346336746551232639353690442167338253911249472180846293240627636206936417230238834629969826759857404361620733529783190017431497148868116235705540170547183515469169233486143520189196748";  
    //System.out.println(greatestProduct(productList(parseListOfStrings(chopString(data)), 0, 3)));
    System.out.println(greatestProduct(productListVertical(parseListOfStrings(chopString(data)), 0, 3, 20)));

  }

  public static ArrayList<String> chopString(String s) {
      String choppyBoi = new String(s);
      ArrayList<String> result = new ArrayList<>();
      while (choppyBoi.length() > 1) {
        result.add(choppyBoi.substring(0,2));
        choppyBoi = choppyBoi.substring(2);
      }
      //result.add(choppyBoi);
      return result;
    }

    public static ArrayList<Integer> parseListOfStrings(ArrayList<String> s) {
      ArrayList<Integer> result = new ArrayList<>();
      for (String strung : s) {
        result.add(Integer.parseInt(strung));
      }
      return result;
    }
    public static int greatestProduct(ArrayList<Integer> list){
      int biggestNum = 1;
      for(int i = 0; i < list.size(); i++){

        if(list.get(i) > biggestNum){
          biggestNum = list.get(i);
        }
      }
      return biggestNum;

    }

    public static ArrayList<Integer> productListVertical(ArrayList<Integer> myLi, int min, int max, int rowLen){

      ArrayList<Integer> runningList = new   ArrayList<Integer>();
      for(int i = min; i < myLi.size() - max; i++){

        runningList.add(productSummation(getDigitsVertical(myLi, min  + i, max + i, rowLen)));

      }
      return runningList;

  }

    public static int productSummation(ArrayList<Integer> myList){

    int runningResult = 1;
    for(int i = 0; i < myList.size(); i++){

      runningResult *= myList.get(i);

    }
    return runningResult;

  }

  public static ArrayList<Integer> getDigitsVertical(ArrayList<Integer> myList, int min, int max, int rowLen){

    ArrayList<Integer> runningResult = new ArrayList<Integer>();
    int c = 1;

    for(int i = min; i <= max * rowLen; i+= rowLen){

      c = myList.get(i);
      runningResult.add(c);
      if(min == (myList.size() - max)){
      return runningResult;
    }

    }
    return runningResult;

  }

}

为什么会出现此错误以及如何修复它?

最佳答案

从异常中可以清楚地看出,您正在尝试检查仅包含 400 个元素的数组中的索引 418。

productListVertical() ,变量i从 0 运行到 400-3=397 。为什么?

然后你运行productSummation()getDigitsVertical(myLi, min + i, max + i, rowLen) 中的每个值,因此参数 1 从 0 到 397,参数 2 从 3 到 400。

方法getDigitsVertical()然后有一个从 i = min 到 max*rowLen 运行的循环。您的 rowLen 是 20,因此您运行的循环最多为 400*20 = 8000 。然后你尝试get(i)对于 i 的值最多 8000 个...在大小为 400 的数组中。

当然,你的狗屎会被毁掉。

需要考虑的教训: 在纸上设计你的算法。如果该算法对您来说没有意义,那么就不要指望它对机器有意义。

注释您的代码,以便其他人可以帮助您。

关于java - 为什么我的垂直乘积求和程序出现 "index out of bounds"异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58845757/

相关文章:

java - 解析多级括号之间的字符串到节点

c - 在结构内部,如何定义与结构本身类型相同的结构数组(动态)

java - 二叉搜索树和 AVLTree 问题

Python按钮算法

java - 适用于 Google App Engine 的最佳 Java 文本索引库是什么?

java - onClick 方法的 View 在转换为 TextView 时返回 null

java - 替换 AuthenticationException.getAuthentication();

c# - 如何使用 MongoDB 访问深层嵌套数组(ASP.NET Core 2.2)

在 C 中复制一个带有字符串成员的结构

java - 在 Java 中搜索树中的节点