ArrayIndexOutOfBoundsException 的数组足够大,可以容纳我提供的数据
我做了以下方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其他人指出了数组问题,但我根本不明白你为什么要使用数组。只需使用
substring
:坦率地说,无论如何,这看起来都是一个奇怪的设计......您确定首先这是正确的行为吗?鉴于您正在处理数字,根本不清楚为什么需要字符串表示形式。根据字符串的预期长度,我期望这样的结果:
例如,这会将 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
: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:
This will split 123456 into 123 and 456, for example. Adjust the 1000 according to your real values.
您的
<=
可能应该是<
以避免该异常。Your
<=
should probably be a<
to avoid that exception.您的 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
当你这么做的时候:
假设 z1.length 是 7,z2.length 是 4,这意味着你的程序将会崩溃,因为 z2 没有索引 4,最多只有 3。
我认为你应该开始你的 counter = 0 并在 count < > 时进行迭代z2.length 如果要将第一个数组的第一个位置复制到第二个数组;
THe moment you do it:
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;