为什么这样写会报错?public class Test
{
static String[] str1 = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10"};

static String make() {
String value = "";
for (int i=0; i<str1.length; i++) {
for (int j=0; i<str1.length; j++) {
if(str1[i].equals(str1[j]) == false){
value = value + str1[i] + " " + str1[j] + "\n";
}
}
}
return value;
}

public static void main(String[] args) {
System.out.print(make());
}
}报错如下:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Test.make(Test.java:9)
at Test.main(Test.java:18)

解决方案 »

  1.   

    因为j的范围没有限制。已经改好了package com.lan.test;public class Test {
    static String[] str1 = { "01", "02", "03", "04", "05", "06", "07", "08",
    "09", "10" };
    static String make() {
    String value = "";
    for (int i = 0; i < str1.length; i++) {
    for (int j = 0; j < str1.length; j++) {//LZ这里的j写成i了
    if (str1[i].equals(str1[j]) == false) {
    value = value + str1[i] + " " + str1[j] + "\n";
    }
    }
    }
    return value;
    } public static void main(String[] args) {
    System.out.print(make());
    }
    }
      

  2.   

    for (int i=0; i<str1.length; i++) {
                for (int j=0; i<str1.length; j++) {
      

  3.   

    for (int i=0; i<str1.length; i++) {
      for (int j=0; i<str1.length; j++) {
    我现在知道了什么叫马马虎虎
      

  4.   

    for (int i=0; i<str1.length; i++) {
      for (int j=0; i<str1.length; j++) {
    呵呵 一般看不出来
      

  5.   

    LZ你要慎重呀..for (int j = 0; j < str1.length; j++) {//LZ这里的j写成i了
                    if (str1[i].equals(str1[j]) == false) {
                        value = value + str1[i] + " " + str1[j] + "\n";
                    }
                }