【问题标题】:Out of bounds. Trying to access index 98 for no apparent reason越界。无缘无故地试图访问索引 98
【发布时间】:2012-01-17 13:18:33
【问题描述】:

作为对我初学者编程能力的挑战,我认为看看我是否可以编写一个简单的蛮力密码事情会很有趣。因此,我开始编写一个应用程序,该应用程序在给定字符串长度值的情况下生成它可能采用的每个字母数字排列。但是,由于我是一个完整的编程新手,所以我遇到了麻烦。

首先,尽管导入了 java.lang.Math,但我收到错误消息,提示找不到符号:pow。我设法通过写出完整的 java.lang.Math.pow(); 来解决这个问题;当我使用该函数时,但为什么它有效但导入不起作用是我无法理解的。

其次,不管输入的长度如何,输入后我得到运行时错误:

aaException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 98
at combination.main(combination.java:53)

这表明在第 53 行:

current[j] = alphanum[((int)current[j])+1];

我显然试图在 current[] 或 alphanum[] 中访问索引 98? 据我所知,这不应该发生......

我对这种发展感到相当困惑。无论如何,这是我的代码:

//48-57 65-90 97-122

import java.util.Scanner;
import java.lang.Math;

public class combination {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        //Alphanum will be an array of chars: the lowercase letters of the alphabet, the uppercase, and the numbers 0-9.
        char[] alphanum = new char[62];

        //Set indexes 0-25 as lowercase a-z, and indexes 26-51 as uppercase A-Z, using ascii conversion.
        for (int i=0; i<26; i++) {
            alphanum[i] = (char)(i+97);
            alphanum[i+26] = (char)(i+65);
        }

        //Set indexes 51-61 as 0-9.
        for (int i=0; i<10; i++) {
            alphanum[i+52] = (char)(i+48);
        }

        //Take in variable for length.
        System.out.print("Enter length: ");
        int length = in.nextInt();

        //Current will be an array of chars: it will hold the current permutation being generated.
        char[] current = new char[length];

        //Set all indexes in current to "a" by default, and print this string as the first permutation.
        for (int i=0; i<length; i++) {
            current[i] = alphanum[0];
            System.out.print(current[i]);
        }

        //power will be a temporary double, used to calculate the number of iterations needed, as the pow function works with doubles.
        double power = (java.lang.Math.pow(62.00, ((double)length)));

        //Convert power to an integer, iterations, and subtract 1 because one iteration was already printed previously.
        int iterations = ((int)power)-1;

        /*The loop works like this. The rightmost char is checked, and if it is the maximum value of the idex
        it is reverted to idex 0 again and the index value of the char to the left of it is increased by 1,
        if it is not the maximum then it is just increased by 1. This is iterated the right number of times such
        that every alphanumeric permutation of that length has been returned.*/
        for (int i=0; i<iterations; i++) {
            for (int j=(length-1); j>=0; j--) {
                if ((j!=0) && (((int)current[j])==122)) {
                    current[j] = alphanum[0];
                    current[j-1] = alphanum[((int)current[j-1])+1];
                } else if (j!=0) {
                    current[j] = alphanum[((int)current[j])+1];
                } else {
                    System.out.println("This has looped too many times. Something is wrong.");
                }
            }

            //At the end of each iteration, print the string.
            for (int l=0; l<length; l++) {
                System.out.print(current[l]);
            }
        }
    }
}

非常感谢您提供的任何帮助或见解。 ^_^

【问题讨论】:

  • pow 的错误是(我猜)因为它是一个静态函数,你必须使用 Math.pow(x,y) 或 import static java.lang.Math
  • 为什么不调试问题?
  • @Thomas 我只是使用 notepad++ 和命令行 javac 来编写和编译。 notepad++ 是否有 java 的功能?
  • 显然不是,但是您可以将 System.out.println() 添加到您的代码中,并通过将变量值打印到控制台来进行调试。
  • 完全同意。如果您不能进行简单的调试,例如使用 System.out.println() 来查看相关变量的值,那么任何人都如何期望编程。基本调试是您需要学习的第一项技能。

标签: java arrays import indexoutofboundsexception


【解决方案1】:

您的alphanum 数组大小为62,((int)current[j-1])+1 的含义是98 (> 62)。

char 'a' 的 int 值为 97。

【讨论】:

  • 啊!是的。我知道我现在哪里出错了。愚蠢的错误。谢谢。
  • 没有聪明的错误。但即使是最聪明的人也会制造它们。这是我们的天性。
【解决方案2】:

我显然试图在 current[] 或 字母[]?据我所知,这不应该发生......

这是完全可能的,因为您尝试在某个索引处访问 alphanum 中的元素,该索引源自当前数组的内容。我建议你在不同的步骤打印出这些数组的内容,你会很快发现你的代码在哪里表现得不像你想象的那样

current[j] = alphanum[((int)current[j])+1];

你在这里尝试访问

int index = ((int)current[j])+1;
current[j] = alphanum[index];

索引似乎是 98

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多