鍦ㄩ�� Stack Overflow 鐨勬椂鍊欙紝鍙戠幇浜嗕竴浜涜闂噺鍍忔槅浠戝北涓�鏍烽珮鐨勯棶棰橈紝姣斿璇磋繖涓細涓轰粈涔堜細鍙戠敓 ArrayIndexOutOfBoundsException
锛熻繖鏍风湅浼肩畝鍗曞埌涓嶅�煎緱涓�闂殑闂锛岃闂噺瓒宠冻鏈� 69涓�+锛岃繖涓嶅緱浜嗗晩锛佽鏄庢湁涓嶅皯鐨勫垵绾х▼搴忓憳琚繖涓棶棰樺洶鎵拌繃銆傚疄璇濆疄璇村惂锛�鎴�涔熸湁鐐瑰悆涓嶅噯涓轰粈涔堛��
鏉ュ洖椤句竴涓嬫彁闂�呯殑闂锛�
ArrayIndexOutOfBoundsException
绌剁珶鎰忓懗鐫�浠�涔堬紵鎴戣濡備綍鎽嗚劚杩欎釜閿欒銆�
濡傛灉浣犱篃鏇捐杩欎釜闂鍥版壈杩囷紝鎴栬�呮鍦ㄨ鍥版壈锛屽氨璇烽殢鎴戜竴璧锋潵姊崇悊涓�涓嬮棶棰樼殑绛旀銆傛墦鎬繘闃跺柦锛�
鏉ョ湅杩欐牱涓�娈典唬鐮侊紝瀹冨氨鍙互寮曡捣 ArrayIndexOutOfBoundsException
銆�
String[] names = { "娌�", "榛�", "鐜�", "浜�" };
for (int i = 0; i <= names.length; i++) {
System.out.println(names[i]);
}
閿欒鐨勫爢鏍堜俊鎭涓嬫墍绀恒��
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.cmower.java_demo.stackoverflow.Cmower1.main(Cmower1.java:7)
鎶涘嚭杩欎釜閿欒鐨勫師鍥犳槸鐢变簬鏁扮粍浣跨敤浜嗛潪娉曠殑涓嬫爣璁块棶锛屾瘮濡傝涓嬫爣涓鸿礋鏁版垨鑰呭ぇ浜庢垨鑰呯瓑浜庢暟缁勭殑闀垮害銆�
鍥犱负鏁扮粍 names 鐨勯暱搴︿负 4锛屼絾涓嬫爣鐨勮捣濮嬩綅缃负 0锛岃�屼笉鏄� 1锛屽鑷� names[4] 鐨勬椂鍊欒秺鐣屼簡銆傝繖涓棶棰樼殑淇鏂规硶铔畝鍗曠殑锛屽氨鏄妸 <=
鏀逛负 <
銆�
String[] names = { "娌�", "榛�", "鐜�", "浜�" };
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
褰� i
涓� 4 鐨勬椂鍊欒璺冲嚭 for 寰幆锛宯ames 鐨勬渶澶т笅鏍囧�间负 3 鑰屼笉鏄� 4銆�
Java 鐨勪笅鏍囬兘鏄粠 0 寮�濮嬬紪鍙风殑锛堟垜涓嶇‘瀹氭湁娌℃湁浠� 1 寮�濮嬬殑缂栫▼璇█锛夛紝杩欏拰鎴戜滑骞冲父鐢熸椿涓粠 1 寮�濮嬬紪鍙风殑涔犳儻涓嶅悓銆侸ava 杩欐牱鍋氱殑鍘熷洜濡備笅锛�
Java 鏄熀浜� C 璇█瀹炵幇鐨勶紝鑰� C 璇█鐨勪笅鏍囨槸浠� 0 寮�濮嬬殑鈥斺�旇繖鍚捣鏉ュソ鍍忔槸涓�鍙ュ簾璇濄�傜湡姝g殑鍘熷洜鏄笅鏍囧苟涓嶆槸涓嬫爣锛屽湪鎸囬拡锛圕锛夎瑷�涓紝瀹冨疄闄呬笂鏄竴涓亸绉婚噺锛岃窛绂诲紑濮嬩綅缃殑涓�涓亸绉婚噺銆傜涓�涓厓绱犲湪寮�澶达紝鍥犳瀹冪殑鍋忕Щ閲忓氨涓� 0銆�
姝ゅ锛岃繕鏈夊彟澶栦竴绉嶈娉曘�傛棭鏈熺殑璁$畻鏈鸿祫婧愭瘮杈冨尞涔忥紝0 浣滀负璧峰涓嬫爣鐩告瘮杈冧簬 1 浣滀负璧峰涓嬫爣锛岀紪璇戠殑鏁堢巼鏇撮珮銆�
姣斿璇达紝10 涓厓绱犵殑鏁扮粍鍏剁粨鏋勫涓嬪浘鎵�绀恒�傜紪鍙蜂粠 0 寮�濮嬶紝绗� 9 涓厓绱犲皢鍦ㄤ笅鏍� 8 澶勮闂��
涓轰簡鎽嗚劚 ArrayIndexOutOfBoundsException
鐨勫洶鎵帮紝闄や簡 i < 0; i < names.length
锛涜繕鏈変竴绉嶆洿鍊煎緱鎺ㄨ崘鐨勫仛娉曗�斺�斾娇鐢ㄥ寮虹殑 for 寰幆锛屽綋鎴戜滑纭畾涓嶉渶瑕佷娇鐢ㄤ笅鏍囩殑鏃跺�欍��
String[] names = { "娌�", "榛�", "鐜�", "浜�" };
for (String name : names) {
System.out.println(name);
}
澧炲己鐨� for 寰幆锛屽交搴曞湴鐢╂帀浜嗕娇鐢ㄦ暟缁勪笅鏍囩殑鍙兘鎬э紝涔熷氨褰诲簳鍦版憜鑴变簡 ArrayIndexOutOfBoundsException
銆傝櫧鐒惰繖鍙槸閽堝鎴戜滑寮�鍙戣�呮潵璇淬��
瀹為檯涓婏紝Java 浼氭妸澧炲己鐨� for 寰幆璇彞瑙i噴涓烘櫘閫氱殑 for 寰幆璇彞锛屼粛鐒朵細浣跨敤涓嬫爣銆�
String[] names = new String[]{"娌�", "榛�", "鐜�", "浜�"};
String[] var2 = names;
int var3 = names.length;
for(int var4 = 0; var4 < var3; ++var4) {
String name = var2[var4];
System.out.println(name);
}
涓嬫爣 var4 鐨勮捣濮嬪�间负 0锛寁ar3 涓烘暟缁勭殑闀垮害锛涘綋 var4 鑷闀夸负 4 鐨勬椂鍊欙紝鍙戠幇 var4 涓嶅皬浜� var3锛屼簬鏄惊鐜��鍑恒��
浣嗕笉绠℃�庝箞璇达紝澧炲己鐨� for 寰幆鐨勭‘涓烘垜浠紑鍙戣�呭甫鏉ヤ簡绂忛煶鈥斺�旀湁鏁堝湴鎽嗚劚浜� ArrayIndexOutOfBoundsException
銆�
鏉ュ姣斾竴涓嬫櫘閫氱殑 for 寰幆鍜屽弽缂栬瘧鍚庣殑澧炲己 for 寰幆锛岀湅鐪嬪畠浠箣闂存湁浠�涔堝尯鍒��
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
int var3 = names.length;
for(int var4 = 0; var4 < var3; ++var4) {
String name = var2[var4];
System.out.println(name);
}
浠庢�ц兘鐨勮搴︽潵鐪嬶紝宸埆涓昏鏈変袱鐐广��
1锛夊寮虹殑 for 寰幆鍦ㄩ亶鍘嗕箣鍓嶈幏鍙栦簡鏁扮粍鐨勯暱搴︼紝骞朵繚瀛樺埌浜嗕竴涓复鏃跺彉閲� var3 涓紝杩欏氨閬垮厤浜嗘瘡娆″惊鐜殑鏃跺�欏啀鍘昏幏鍙栦竴娆℃暟缁勯暱搴︺��
2锛夊寮虹殑 for 寰幆浣跨敤浜嗗墠缃嚜澧� ++var4
锛岃�屾櫘閫氱殑 for 寰幆浣跨敤浜嗗悗缃嚜澧� i++
銆傝繖涓よ�呬箣闂存槸鏈変竴瀹氱殑宸埆鐨勶紝鎰熷叴瓒g殑鍚屽鍙互浜嗚В涓�涓嬨��
濡傛灉浣跨敤鐨勬槸 JDK8 浠ヤ笂鐨勭増鏈紝鎴戜滑杩樺彲浠ヨ繖鏍烽亶鍘嗘暟缁勶紙涓嶄娇鐢ㄤ笅鏍囷級銆�
绗竴绉嶏細浣跨敤 List.forEach
銆�
Arrays.asList(names).forEach(System.out::println);
绗簩绉嶏細浣跨敤 Stream
銆�
Stream.of(names).forEach(System.out::println);
濡傛灉闇�瑕佸鏁扮粍鎵ц鍏朵粬鎿嶄綔锛屾瘮濡傝杩囨护绛夋搷浣滐紝鍙互灏嗘暟缁勮浆鎹负鈥滄祦鈥濄��
杩欎袱绉嶅仛娉曢兘闇�瑕佺敤鍒� forEach()
鏂规硶锛岃鏂规硶鍏跺疄鏄�氳繃澧炲己鐨� for 寰幆瀹炵幇鐨勶紝婧愮爜濡備笅鎵�绀恒��
public void forEach(Consumer super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
璇村埌搴曪紝濡傛灉鎯宠鎽嗚劚 ArrayIndexOutOfBoundsException
鐨勫洶鎵帮紝浣跨敤澧炲己鐨� for 寰幆鏉ラ亶鍘嗘暟缁勫氨瀵逛簡銆傛妸鎴戜滑寮�鍙戣�呭鏄撶枏蹇界殑閿欒锛堟瘮濡� i <= names.length
锛変氦缁欐櫤鑳藉寲鐨勭紪璇戝櫒鏉ュ鐞嗭紝灏辨槸鏈�濂界殑鍔炴硶銆�
濂戒簡鍚勪綅璇昏�呮湅鍙嬩滑锛屼互涓婂氨鏄湰鏂囩殑鍏ㄩ儴鍐呭浜嗐��鑳界湅鍒拌繖閲岀殑閮芥槸浜烘墠锛屼簩鍝ュ繀椤昏涓轰綘鐐逛釜璧�馃憤銆傚鏋滆寰椾笉杩囩樉锛岃繕鎯崇湅鍒版洿澶氾紝鎴戝啀鎺ㄨ崘鍑犵瘒缁欏ぇ瀹躲��
Stack Overflow涓�188涓囨祻瑙堥噺鐨勬彁闂細Java 鍒板簳鏄�间紶閫掕繕鏄紩鐢ㄤ紶閫掞紵
Stack Overflow 涓� 370涓囨祻瑙堥噺鐨勪竴涓棶棰橈細濡備綍姣旇緝 Java 鐨勫瓧绗︿覆锛�
Stack Overflow 涓� 250涓囨祻瑙堥噺鐨勪竴涓棶棰橈細浠�涔堟槸 NullPointerException
鍏绘垚濂戒範鎯紒濡傛灉瑙夊緱杩欑瘒鏂囩珷鏈夌偣鐢ㄧ殑璇濓紝姹傜偣璧炪�佹眰鍏虫敞銆佹眰鍒嗕韩銆佹眰鐣欒█锛岃繖灏嗘槸鎴戝啓涓嬪幓鐨勬渶寮哄姩鍔涳紒