[英]Why is this program giving me an array index out of bounds exception?
static char characters[] = { 'a', 'b', ' ', 'c', ' ', 'e', 'f', ' ' };
public static void main(String args[]) {
for (int i = 0; i < characters.length; i++) {
int j = i + 1;
if (characters[i] == ' ') {
while (j < characters.length && characters[j] == ' ' || characters[j] == '\u0000') {
j++; // increment j till a non-space char is found
}
}
}
for (char character : characters) {
System.out.print(character + " ");
}
}
Eclipse shows the following error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8 at loopchecker.main(loopchecker.java:13) Eclipse显示以下错误:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:8在loopchecker.main(loopchecker.java:13)
Line 13 is the while loop
. 第13行是
while loop
。 But I am already checking if j's
value is less than the length or not. 但我已经在检查
j's
值是否小于长度。
It´s because of the or
operator. 这是因为
or
运营商。
The following condition reads like that 以下条件就是这样的
(j < characters.length && characters[j] == ' ') || characters[j] == '\u0000'
so you allways execute the or condition due to the precedence of the and
operator above the or
operator which causes the ArrayOutOfBoundException.
所以你永诺执行或病症因的优先级
and
操作者以上or
导致操作者ArrayOutOfBoundException.
You might want to include braces in your while condition. 您可能希望在while条件中包含大括号。
while (j < characters.length && (characters[j] == ' ' || characters[j] == '\u0000'))
You need to add some parentheses to your while
statement: 您需要在
while
语句中添加一些括号:
while (j<characters.length && (characters[j] == ' ' || characters[j] == '\u0000'))
Otherwise, Java's order of operations sees it as 否则,Java的操作顺序将其视为
while ((j<characters.length && characters[j] == ' ') || characters[j] == '\u0000')
which means characters[j]
will still be evaluated if the first portion is false. 这意味着如果第一部分为假,仍将评估
characters[j]
。
Given that in the comment, you want to say //increment j till a non-space char is found
, I would say that you should get rid of: 鉴于在评论中,你想说
//increment j till a non-space char is found
,我会说你应该摆脱:
while (j<characters.length && characters[j] == ' ' || characters[j] == '\u0000')
Which is causing your error due to the problem with the ||
由于
||
的问题导致您的错误 being evaluated after the &&
and replace it with: 在
&&
之后进行评估并将其替换为:
while (j<characters.length && Character.isWhitespace(characters[j]))
This solves your problem along with dealing with the whitespace issue much more conveniently. 这样可以更方便地解决您的问题以及处理空白问题。
The characters[j] == '\ '
part doesn't have a check to ensure that j is less than the length. characters[j] == '\ '
部分没有检查以确保j小于长度。 Put something like j < characters.length && (.. || ..)
放点
j < characters.length && (.. || ..)
It is because of the OR operator . 这是因为OR运算符。 Below is the complete solution .
以下是完整的解决方案。
package com.java;
public class ArraySample
{
static char characters[]={'a','b',' ','c',' ','e','f',' '};
public static void main (String args[])
{
for (int i = 0; i < characters.length; i++) {
int j = i + 1;
if (characters[i] == ' ') {
while (j < characters.length && (characters[j] == ' ' || characters[j] == '\u0000')) {
j++; //increment j till a non-space char is found
}
}
}
for(int i = 0; i< characters.length; i++)
{
System.out.print(characters[i]+" ");
}
}
}
static char characters[] = { 'a', 'b', ' ', 'c', ' ', 'e', 'f', 's' };
public static void main(String args[]) {
for (int i = 0; i < characters.length; i++) {
int j = i - 1 ; // The problem was here J was surpassing the characters length
if (characters[i] == ' ') {
while (j < characters.length && characters[j] == ' ' || characters[j] == '\u0000') {
j++; // increment j till a non-space char is found
}
}
}
for (char character : characters) {
System.out.print(character + " ");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.