为什么它显示错误就像我认为第三个循环将结束,它将进入第一个循环,但没有其他显示异常

oyxsuwqo  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(341)

我不明白为什么循环时会显示异常:
线程“main”java.lang.arrayindexoutofboundsexception中出现异常:索引26超出循环处长度26的界限。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]);

        }
      }

    }
  }
}
bmvo0sr5

bmvo0sr51#

只需用26替换there循环的条件。现在将不会发生异常索引越界
建议应用程序名的公共类{
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]);

    }
  }

}

} }

相关问题