简体   繁体   English

具有最小值/最大值的 ArrayOutOfBounds

[英]ArrayOutOfBounds with minimum/maximum values

I am trying to solve Project Euler problem 18. I have created an array for each row (starting from the bottom), and then an array of those arrays.我正在尝试解决 Project Euler 问题 18。我为每一行(从底部开始)创建了一个数组,然后是这些数组的数组。 I created a recursive method that starts from the bottom row, and looks ahead three rows to find the best path.我创建了一个从底行开始的递归方法,向前看三行以找到最佳路径。

I created minimum and maximum methods to make sure that the index of my arrays could not go below zero, or above the length minus one.我创建了最小和最大方法来确保我的数组的索引不能低于零,或高于长度减一。

/**
 * A method that sets a minimum limit for an integer
 * @param a The number
 * @param b The lowest value it can go
 * @return a
 */
public static int min(int a, int b) {
    if (a<b) {
        a=b;
    }
    return a;
}
/**
 * Sets the maximum limit for an int
 * @param a the number
 * @param b The highest a number can go
 * @return a
 */
public static int max(int a, int b) {
    if(a>b) {
        a=b;
    }
    return a;
}

Then I used these methods when calculating all possible paths in the next three rows.然后我在计算接下来三行中所有可能的路径时使用这些方法。 I got the Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 error on this line of code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11中的这一行代码中得到了Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11

eighthPoss = array[x+2][max(i, array[x].length-1)] + array[x+1][max(i, array[x].length-1)] + array[x][max(i, array[x].length-1)];

Where x is the current row, and i is the current position on the row.其中x是当前行, i是该行的当前位置。 I have if statements for when x+2 and x+1 are more than the amount of rows (When we are on the second to last, or last row of the triangle).x+2x+1大于行数时,我有if语句(当我们在倒数第二行或三角形的最后一行时)。 I am genuinely confused how anything on this line of code is out of bounds as I have minimum and maximum values on each of them to make sure they don't go out of the range.我真的很困惑这行代码中的任何内容是如何超出范围的,因为我对每个代码都有最小值和最大值以确保它们不会超出范围。 I ran print statements and the last numbers the loops ran through before the error were x=1 , i=10 .我运行了打印语句,错误之前循环运行的最后一个数字是x=1 , i=10 Below are my arrays.下面是我的数组。 (I did not include the top row, since it is only one number.) (我没有包括第一行,因为它只有一个数字。)

int[] row1 = {04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23};
int[] row2 = {63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31};
int[] row3 = {91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48};
int[] row4 = {70, 11, 33 ,28, 77, 73, 17, 78, 39, 68, 17, 57};
int[] row5 = {53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14};
int[] row6 = {41, 48, 72, 33, 47, 32, 37, 16, 94, 29};
int[] row7 = {41, 41, 26, 56, 83, 40, 80, 70, 33};
int[] row8 = {99, 65, 4, 28, 6, 16, 70, 92};
int[] row9 = {88, 2, 77, 73, 7, 63, 67};
int[] row10 = {19, 1, 23, 75, 3, 34};
int[] row11 = {20, 4, 82, 47, 65};
int[] row12 = {18, 35, 87, 10};
int[] row13 = {17, 47, 82};
int[] row14 = {95, 64};

int[][] rows = {row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11, row12, 
                row13, row14};

Any help you could give me would be greatly appreciated.您能给我的任何帮助将不胜感激。

Make sure that your x+2 isn't going out of bounds as it seems you're only checking to see if i is going out of bounds.确保您的x+2没有越界,因为您似乎只是在检查i是否越界。 (You may be checking for it somewhere else but you didn't provide that code). (您可能正在其他地方检查它,但您没有提供该代码)。

Bonus clamp method that I personally use instead of individual max and min methods (since they're built into Java).我个人使用的奖励钳位方法而不是单独的 max 和 min 方法(因为它们内置于 Java 中)。

public static int clamp(int a, int min, int max) {
    return Math.max(min, Math.min(max, a));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM