为什么显示错误。就像我认为第三个循环将结束并且它将进入第一个循环但没有显示异常
why is it showing error . Like I thought the third loop will end and it will enter the first loop but didn't else showed an exception
我不明白为什么它在循环时显示 that exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 26 out of bounds for length 26 at
loops.SuggestingAppNames.main(SuggestingAppNames.java:29)
我想要所有可能的英文字母排列。
public class SuggestingAppNames {
public static void main(String[] args) {
System.out.println("the possible outcomes are");
String a = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String d[] = a.split(",");
String b = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String e[] = b.split(",");
String c = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String f[] = c.split(",");
for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < b.length(); j++) {
for (int k = 0; k < c.length(); k++) {
System.out.println(d[i] + e[j] + f[k]);
}
}
}
}
}
只需将there循环的条件替换为26即可,这样索引越界的异常就不会出现了
public class SuggestingAppNames {
public static void main(String[] args) {
System.out.println("可能的结果是");
String a = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String d[] = a.split(",");
String b = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String e[] = b.split(",");
String c = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String f[] = c.split(",");
for (int i = 0; i < 26 ; i++) {
for (int j = 0; j < 26; j++) {
for (int k = 0; k < 26; k++) {
System.out.println(d[i] + e[j] + f[k]);
}
}
}
}
}
我不明白为什么它在循环时显示 that exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 26 out of bounds for length 26 at loops.SuggestingAppNames.main(SuggestingAppNames.java:29)
我想要所有可能的英文字母排列。
public class SuggestingAppNames {
public static void main(String[] args) {
System.out.println("the possible outcomes are");
String a = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String d[] = a.split(",");
String b = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String e[] = b.split(",");
String c = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String f[] = c.split(",");
for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < b.length(); j++) {
for (int k = 0; k < c.length(); k++) {
System.out.println(d[i] + e[j] + f[k]);
}
}
}
}
}
只需将there循环的条件替换为26即可,这样索引越界的异常就不会出现了
public class SuggestingAppNames {
public static void main(String[] args) { System.out.println("可能的结果是");
String a = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String d[] = a.split(",");
String b = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String e[] = b.split(",");
String c = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
String f[] = c.split(",");
for (int i = 0; i < 26 ; i++) {
for (int j = 0; j < 26; j++) {
for (int k = 0; k < 26; k++) {
System.out.println(d[i] + e[j] + f[k]);
}
}
}
} }