天天看點

java 數組越界_JAVA 數組越界 怎麼破

Exceptioninthread"main"java.lang.ArrayIndexOutOfBoundsException:0atStudentTest.StudentTest2.main(StudentTest2.java:9)下面是代碼:publicclassStudentTest2{publicstaticvoidm...

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

at StudentTest.StudentTest2.main(StudentTest2.java:9)

下面是代碼:

public class StudentTest2 {

public static void main(String[] args) {

Student[]Student=new Student[3];

Student[0]= new Student("張三",8, "三年級", "廣州天河", "[email protected]", "15102092822");

Student[1]= new Student("李四",9, "三年級", "廣州荔灣", "[email protected]", "13542091180");

Student[2]= new Student("王五",8, "四年級", "廣州黃埔", "[email protected]", "17324069804");

String str = args[0];

int Find=0;

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

if (str.equals(Student[i].getName())||

str.equals(Student[i].getEmail())||

str.equals(Student[i].getAddress())) {

System.out.println("你查找到了"+Student[i].getName());

Find++;

}

}

if (Find==0) {

System.out.println("查無此人!");

}

}

}

class Student {

private String name, gender, address, email;

private int age;

private String phone;

public Student() {

this.name = "nobody";

this.age = 0;

this.gender = "男";

this.phone = "10086";

this.address = "address";

this.email = "[email protected]";

}

public Student(String name,int age,String gender,String address,String email,String phone) {

this.name = name;

this.age = age;

this.gender = gender;

this.address = address;

this.email = email;

this.phone = phone;

}

public void setName(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setGender(String gender) {

this.gender = gender;

}

public String getGender() {

return gender;

}

public void setAddress(String address) {

this.address = address;

}

public String getAddress() {

return address;

}

public void setEmail(String email) {

this.email = email;

}

public String getEmail() {

return email;

}

public void setAge(int age) {

this.age = age;

}

public int getAge() {

return age;

}

public void setPhone(String phone) {

this.phone = phone;

}

public String getPhone() {

return phone;

}

public void eat() {

System.out.println("我會吃飯");

}

public void sleep() {

System.out.println("我會睡覺");

}

public void play() {

System.out.println("我會玩");

}

public void driver() {

System.out.println("我會喝");

}

}

展開