Stack Overflow涓�59涓囨祻瑙堥噺鐨勬彁闂細涓轰粈涔堜細鍙戠敓ArrayIndexOutOfBoundsException锛�

鍦ㄩ�� 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 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

鍏绘垚濂戒範鎯紒濡傛灉瑙夊緱杩欑瘒鏂囩珷鏈夌偣鐢ㄧ殑璇濓紝姹傜偣璧炪�佹眰鍏虫敞銆佹眰鍒嗕韩銆佹眰鐣欒█锛岃繖灏嗘槸鎴戝啓涓嬪幓鐨勬渶寮哄姩鍔涳紒

你可能感兴趣的:(Stack Overflow涓�59涓囨祻瑙堥噺鐨勬彁闂細涓轰粈涔堜細鍙戠敓ArrayIndexOutOfBoundsException锛�)