数组索引越界异常 (Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18549869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array Index Out of Bounds Exception (Java)
提问by Boxasauras
Here is my code:
这是我的代码:
public class countChar {
public static void main(String[] args) {
int i;
String userInput = new String();
userInput = Input.getString("Please enter a sentence");
int[] total = totalChars(userInput.toLowerCase());
for (i = 0; i < total.length; i++);
{
if (total[i] != 0) {
System.out.println("Letter" + (char) ('a' + i) + " count =" + total[i]);
}
}
}
public static int[] totalChars(String userInput) {
int[] total = new int[26];
int i;
for (i = 0; i < userInput.length(); i++) {
if (Character.isLetter(userInput.charAt(i))) {
total[userInput.charAt(i) - 'a']++;
}
}
return total;
}
}
The program's purpose is to ask the user for a string, and then count the number of times each character is used in the string.
该程序的目的是向用户询问一个字符串,然后计算该字符串中每个字符被使用的次数。
When I go to compile the program, it works fine. When I run the program, I am able to enter a string in the popup box, but after I submit the string and press OK, I get an error, saying
当我去编译程序时,它工作正常。当我运行程序时,我可以在弹出框中输入一个字符串,但是在我提交字符串并按 OK 后,我收到一个错误,说
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
at countChar.main(countChar.java:14)
I'm not completely sure what the problem is or how to fix it.
我不完全确定问题是什么或如何解决它。
回答by JB Nizet
for ( i = 0; i < total.length; i++ );
^-- remove the semi-colon here
With this semi-colon, the loop loops until i == total.length
, doing nothing, and then what you thought was the body of the loop is executed.
使用这个分号,循环会循环直到i == total.length
,什么都不做,然后你认为是循环体的内容被执行。
回答by Sotirios Delimanolis
for ( i = 0; i < total.length; i++ ); // remove this
{
if (total[i]!=0)
System.out.println( "Letter" + (char)( 'a' + i) + " count =" + total[i]);
}
The for loop loops until i=26
(where 26 is total.length
) and then your if
is executed, going over the bounds of the array. Remove the ;
at the end of the for
loop.
for 循环循环直到i=26
(其中 26 是total.length
),然后if
执行您的循环,越过数组的边界。删除循环;
末尾的for
。
回答by AyAz
This is Very Good Example of minus Length of an array in java, i am giving here both examples
这是在java中减去数组长度的很好例子,我在这里给出了两个例子
public static int linearSearchArray(){
int[] arrayOFInt = {1,7,5,55,89,1,214,78,2,0,8,2,3,4,7};
int key = 7;
int i = 0;
int count = 0;
for ( i = 0; i< arrayOFInt.length; i++){
if ( arrayOFInt[i] == key ){
System.out.println("Key Found in arrayOFInt = " + arrayOFInt[i] );
count ++;
}
}
System.out.println("this Element found the ("+ count +") number of Times");
return i;
}
this above i < arrayOFInt.length; not need to minus one by length of array; but if you i <= arrayOFInt.length -1; is necessary other wise arrayOutOfIndexException Occur, hope this will help you.
这上面 i < arrayOFInt.length; 不需要按数组长度减一;但如果你 i <= arrayOFInt.length -1; 是必要的其他明智的arrayOutOfIndexException 发生,希望这会帮助你。
回答by vishnu
import java.io.*;
import java.util.Scanner;
class ar1 {
public static void main(String[] args) {
//Scanner sc=new Scanner(System.in);
int[] a={10,20,30,40,12,32};
int bi=0,sm=0;
//bi=sc.nextInt();
//sm=sc.nextInt();
for(int i=0;i<=a.length-1;i++) {
if(a[i]>a[i+1])
bi=a[i];
if(a[i]<a[i+1])
sm=a[i];
}
System.out.println("big"+bi+"small"+sm);
}
}