JAVA 数组越界 怎么破
Exceptioninthread"main"java.lang.ArrayIndexOutOfBoundsException:0atStudentTest.Studen...
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, "三年级", "广州天河", "784664007@qq.com", "15102092822");
Student[1]= new Student("李四",9, "三年级", "广州荔湾", "2219507109@qq.com", "13542091180");
Student[2]= new Student("王五",8, "四年级", "广州黄埔", "1050159318@qq.com", "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 = "alsodragon@gmail.com";
}
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("我会喝");
}
} 展开
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, "三年级", "广州天河", "784664007@qq.com", "15102092822");
Student[1]= new Student("李四",9, "三年级", "广州荔湾", "2219507109@qq.com", "13542091180");
Student[2]= new Student("王五",8, "四年级", "广州黄埔", "1050159318@qq.com", "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 = "alsodragon@gmail.com";
}
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("我会喝");
}
} 展开
2018-11-16
展开全部
在java中main方法是程序的入口。main方法形参变量args是用来接收动态参数。
args数组大小为空。数组下标以0开始,args[0]不存在,所以报数组下标越界。java.lang.ArrayIndexOutOfBoundsException属于运行时异常,编译时期没报错。
参考:用法: java [-options] class [args...]
java StudentTest2 张三
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询