下面這個(gè)為什么會(huì)運(yùn)行失敗,也出來答案了
public class HelloWorld {
? ? public static void main(String[] args) {
// 定義一個(gè)長度為 3 的字符串?dāng)?shù)組,并賦值初始值
String[] hobbys = { "sports", "game", "movie" };
System.out.println("循環(huán)輸出數(shù)組中元素的值:");
// 使用循環(huán)遍歷數(shù)組中的元素
for (int i =0;i<=hobbys.length;i++){
? ? System.out.println(""+hobbys[i]);
}
}
}
運(yùn)行失敗
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at HelloWorld.main(HelloWorld.java:11)
循環(huán)輸出數(shù)組中元素的值:
sports
game
movie
2019-02-01
數(shù)組越界,把等于號(hào)去掉就好了
2019-02-13
因?yàn)閿?shù)組的下表是從0開始的,所以在for循環(huán)時(shí),如果循環(huán)中的變量是從0開始的,要小于該數(shù)組的length
2019-02-08
?String[] hobbys = { "sports", "game", "movie" };
?
??
?
?// 使用循環(huán)遍歷數(shù)組中的元素
?for(int i=0;i<=hobbys.length-1;i++)
?{
?? System.out.println(hobbys[i]);
?}
??
2019-02-02
hobbys.length為3,下標(biāo)0代表數(shù)組中第一個(gè)元素,下標(biāo)3代表數(shù)組中第四個(gè)元素,這個(gè)數(shù)組中只有三個(gè)元素
2019-02-01
把等于號(hào)去掉
2019-02-01
輸出那里把雙引號(hào)和+號(hào)去掉試試