简体   繁体   English

将文件读入二维数组-获取ArrayIndexOutOfBoundsException()

[英]Reading a File into a 2D Array - Getting ArrayIndexOutOfBoundsException()

Hi I've been doing Project Euler problems lately but I'm having trouble with Problem 18, here it is: 嗨,我最近一直在做Project Euler问题,但是我遇到了问题18的麻烦,这里是:

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 通过从下面的三角形的顶部开始并移至下面一行的相邻数字,从上到下的最大总数为23。

3 3

7 4 7 4

2 4 6 2 4 6

8 5 9 3 8 5 9 3

That is, 3 + 7 + 4 + 9 = 23. 即3 + 7 + 4 + 9 = 23。

Find the maximum total from top to bottom of the triangle below: 在下面的三角形的顶部到底部找到最大总数:

75 75

95 64 95 64

17 47 82 17 47 82

18 35 87 10 18 35 87 10

20 04 82 47 65 20 04 82 47 65

19 01 23 75 03 34 19 01 23 75 03 34

88 02 77 73 07 63 67 88 02 77 73 07 63 67

99 65 04 28 06 16 70 92 99 65 04 28 06 16 70 92

41 41 26 56 83 40 80 70 33 41 41 26 56 83 40 80 70 33

41 48 72 33 47 32 37 16 94 29 41 48 72 33 47 32 37 16 94 29

53 71 44 65 25 43 91 52 97 51 14 53 71 44 65 25 43 91 52 97 51 14

70 11 33 28 77 73 17 78 39 68 17 57 70 11 33 28 77 73 17 78 39 68 17 57

91 71 52 38 17 14 91 43 58 50 27 29 48 91 71 52 38 17 14 91 43 58 50 27 29 48

63 66 04 68 89 53 67 30 73 16 69 87 40 31 63 66 04 68 89 53 67 30 73 16 69 87 40 31

04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. 注意:由于只有16384条路线,因此可以通过尝试每条路线来解决此问题。 However, Problem 67, is the same challenge with a triangle containing one-hundred rows; 但是,问题67对于包含一百行的三角形是同样的挑战。 it cannot be solved by brute force, and requires a clever method! 它不能用强力解决,需要一个聪明的方法! ;o) ; o)

My algorithm for finding the max total is correct. 我找到最大总数的算法是正确的。 I tested it by manually entering the numbers in a 2d array and the program ran fine. 我通过在2d数组中手动​​输入数字进行了测试,程序运行正常。 The reason I don't want to do it that way is as this Problem says Problem 67 is the same except much larger numbers and I don't want to be typing numbers all day and also I want to practice manipulating files etc. 我不想那样做的原因是因为该问题说问题67是相同的,除了数字更大以外,我不想整天都键入数字,也不想练习操纵文件等。

Anyway to the point I suppose the best thing I can do really is show you my code and the errors I'm getting. 无论如何,我想我能做的最好的事情就是向您展示我的代码和我遇到的错误。 I did some debugging and it seems to be how I'm converting the string of numbers into an array of digits. 我进行了一些调试,这似乎是将数字字符串转换为数字数组的方式。 When I run the program it gives an ArrayIndexOutOfBoundsException(). 当我运行程序时,它给出了ArrayIndexOutOfBoundsException()。 The .txt file consists of the large group of numbers in the problem description above. .txt文件由上述问题描述中的大量数字组成。

public static void main(String[] args) throws Exception
{
    Problem18 p18 = new Problem18() ;
    p18.maxTotalPath() ;
}

public int[][] readFile() throws Exception
{
    ClassLoader loader = Thread.currentThread().getContextClassLoader() ;
    InputStream file = loader.getResourceAsStream("Triangle.txt") ;

    Scanner scan = new Scanner(file) ;

    int[][] triangle = new int[15][15] ;
    int m = 0, n = 0 ;
    String line ;
    while(scan.hasNext())
    {
        line = scan.nextLine() ;
        String[] numbers = line.split(" ") ;

        for(int i = 0 ; i < numbers.length ; i++)
        {
            triangle[m][n] = Integer.parseInt(numbers[i]) ;
            //System.out.print(triangle[m][n] + " ") ;
            n += 1 ;
        }
        n = 0 ;
        // System.out.println("") ;
        m += 1 ;
    }
    scan.close() ;
    return triangle ;
}

public void maxTotalPath() throws Exception
{
    int[][] arr = readFile() ;
    for (int i = arr.length - 2 ; i >= 0 ; i--) 
    {
        for (int j = 0 ; j < arr[i].length; j++) 
        {
            arr[i][j] += Math.max(arr[i + 1][j], arr[i + 1][j + 1]); 
        }
    }
    System.out.println(Integer.toString(arr[0][0])) ;
}

The errors then are: 错误如下:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at com.jconnolly.projeuler.problems.Problem18.maxTotalPath(Problem18.java:83)
at com.jconnolly.projeuler.problems.Problem18.main(Problem18.java:44)

Any help would be much appreciated thanks! 任何帮助将不胜感激谢谢!

maxTotalPath内部循环应转到arr [i] .length-1,这将至少停止异常

Well, your readFile function is working as it should. 好了,您的readFile函数可以正常工作。 Your maxTotalPath is trying to access the wrong memory in the inner loop. 您的maxTotalPath尝试访问内部循环中的错误内存。 If j goes all the way to arr[i].length-1 , then when you access arr[i+1][j+1] , you will be reaching memory beyond the bounds of the array. 如果j一直arr[i].length-1 ,那么当您访问arr[i+1][j+1] ,您将到达超出数组范围的内存。

for (int j = 0 ; j < arr[i].length-1; j++) 
{
    arr[i][j] += Math.max(arr[i + 1][j], arr[i + 1][j + 1]); 
}

Correcting the wrong answer 纠正错误的答案

I think this is what you need to do 我认为这是您需要做的

for (int j = 0 ; j < arr[i].length; j++) 
{
    if (j != arr[i].length-1) {
        arr[i][j] += Math.max(arr[i + 1][j], arr[i + 1][j + 1]); 
    } else {
        arr[i][j] += arr[i+1][j];
    }
}

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

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