ArrayIndexOutOfBoundsException 的数组足够大,可以容纳我提供的数据

发布于 2024-11-18 07:35:52 字数 948 浏览 4 评论 0原文

我做了以下方法:

static int GenerateSeccondPal(int x){
    String y = Integer.toString(x);
    char[] z1 = y.toCharArray();
    char[] z2 = new char[y.length() / 2];

    for (int count = (z1.length /2); count <= z1.length; count++) {
        z2[count] = z1[count];
    }
    return Integer.parseInt(new String(z2));

}

但是,当我运行它时,我收到此错误:

线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException: 3 在挑战.Problem_4.GenerateSeccondPal(Problem_4.java:31) 在 Challenges.Problem_4.main(Problem_4.java:6)

这很奇怪,因为我制作的另一种方法:

static int GenerateFirstPal(int x) {
    String y = Integer.toString(x);
    char[] z1 = y.toCharArray();
    char[] z2 = new char[z1.length / 2];

    for (int count = 0; count < z1.length / 2; count++) {
        z2[count] = z1[count];
    }
    return Integer.parseInt(new String(z2));

}

效果很好。我写的有什么问题吗?

I've made the following method:

static int GenerateSeccondPal(int x){
    String y = Integer.toString(x);
    char[] z1 = y.toCharArray();
    char[] z2 = new char[y.length() / 2];

    for (int count = (z1.length /2); count <= z1.length; count++) {
        z2[count] = z1[count];
    }
    return Integer.parseInt(new String(z2));

}

However, when I run it, I get this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at challenges.Problem_4.GenerateSeccondPal(Problem_4.java:31)
at challenges.Problem_4.main(Problem_4.java:6)

Which is odd, because the other method I made:

static int GenerateFirstPal(int x) {
    String y = Integer.toString(x);
    char[] z1 = y.toCharArray();
    char[] z2 = new char[z1.length / 2];

    for (int count = 0; count < z1.length / 2; count++) {
        z2[count] = z1[count];
    }
    return Integer.parseInt(new String(z2));

}

Works perfectly. What is wrong with what I have written?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯离梦残月幽梦 2024-11-25 07:35:52

其他人指出了数组问题,但我根本不明白你为什么要使用数组。只需使用substring

static int generateFirstPal(int x) {
    String y = String.valueOf(x);
    String firstPart = y.substring(0, y.length() / 2);
    return Integer.parseInt(firstPart);
}

static int generateSecondPal(int x) {
    String y = String.valueOf(x);
    String secondPart = y.substring(y.length() / 2);
    return Integer.parseInt(secondPart);
}

坦率地说,无论如何,这看起来都是一个奇怪的设计......您确定首先这是正确的行为吗?鉴于您正在处理数字,根本不清楚为什么需要字符串表示形式。根据字符串的预期长度,我期望这样的结果:

static int generateFirstPal(int x) {
    return x / 1000;
}

static int generateSecondPal(int x) {
    return x % 1000;
}

例如,这会将 123456 拆分为 123 和 456。根据您的实际值调整 1000。

Others have pointed out the array problems, but I don't see why you're using arrays at all. Just use substring:

static int generateFirstPal(int x) {
    String y = String.valueOf(x);
    String firstPart = y.substring(0, y.length() / 2);
    return Integer.parseInt(firstPart);
}

static int generateSecondPal(int x) {
    String y = String.valueOf(x);
    String secondPart = y.substring(y.length() / 2);
    return Integer.parseInt(secondPart);
}

Frankly this seems like an odd design anyway... are you sure this is the right behaviour in the first place? Given that you're dealing with numbers, it's not clear why you need a string representation at all. Depending on the expected length of the strings, I'd expect something like this:

static int generateFirstPal(int x) {
    return x / 1000;
}

static int generateSecondPal(int x) {
    return x % 1000;
}

This will split 123456 into 123 and 456, for example. Adjust the 1000 according to your real values.

染墨丶若流云 2024-11-25 07:35:52

您的 <= 可能应该是 < 以避免该异常。

Your <= should probably be a < to avoid that exception.

世界等同你 2024-11-25 07:35:52

您的 count 变量不是从 0 开始,因此您必须保留金额,以便 z2[count] 在 for 中从 0 开始,而不是从 z1.length / 2 开始

Your count variable is not starting from 0, so you have to rest the amount so that z2[count] starts from 0 in the for instead of starting from z1.length / 2

云胡 2024-11-25 07:35:52

当你这么做的时候:

for (int count = (z1.length /2); count <= z1.length; count++)
{ 
 z2[count] = z1[count];
} 

假设 z1.length 是 7,z2.length 是 4,这意味着你的程序将会崩溃,因为 z2 没有索引 4,最多只有 3。

我认为你应该开始你的 counter = 0 并在 count < > 时进行迭代z2.length 如果要将第一个数组的第一个位置复制到第二个数组;

THe moment you do it:

for (int count = (z1.length /2); count <= z1.length; count++)
{ 
 z2[count] = z1[count];
} 

Supposing z1.length is 7 and z2.length is 4 it means that your program will crash because z2 does not have an index 4, only 3 at maximum.

I think you should start your counter = 0 and iterate while count < z2.length if you want to copy the first positions of the first array to the second;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文