英文:
Index 0 out of bounds for length 0 at university_info.UniversityDriver.main(UniversityDriver.java:39)
问题
以下是您提供的代码的翻译部分:
我在尝试运行我的程序时遇到了这个错误。
有人可以帮我找出问题吗?
> 在线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:索引0超出了长度0的界限  
> 位于university_info.UniversityDriver.main(UniversityDriver.java:39)
我的代码:
UniversityDriver.java
    包 university_info;
    导入 java.io.FileInputStream;
    导入 java.io.FileNotFoundException;
    导入 java.io.FileOutputStream;
    导入 java.io.IOException;
    导入 java.io.ObjectInputStream;
    导入 java.io.ObjectOutputStream;
    导入 java.util.ArrayList;
    导入 java.util.Scanner;
    
    公共类 UniversityDriver {
    
       公共静态无效 main(String[] args) {
           // TODO 自动生成的方法存根
           大学 u_city = new University("hero university","好好教学");
           System.out.println("欢迎来到" + u_city.universityName + "大学");
           System.out.println(u_city.motto);
         
      
           // 在启动时从外部文件检索数据...
           ArrayList<Person> al = new ArrayList<Person>();
           尝试 {
               FileInputStream fis = new FileInputStream("fileName.txt");
               ObjectInputStream ois = new ObjectInputStream(fis);
               al = (ArrayList<Person>) ois.readObject();
               ois.close();
           } 捕获 (FileNotFoundException ex) {
               System.out.println("文件未找到异常");
           } 捕获 (IOException ex) {
               ex.printStackTrace();
           } 捕获 (ClassNotFoundException ex) {
              
           }
      
           对象[] 数组 = 新对象[al.size()];
           array = al.toArray(array);
      
           对于 (int i = 0; i <= array.length; i++) {
               u_city.people[i] = (Person) array[i];
           }
          
           System.out.println("您想要做什么?");
           System.out.println("输入'雇佣'以雇佣新的教职员工。 ");
           System.out.println("输入'录取'以录取新的学生 ");
           System.out.println("输入'查找学生'以列出有关学生的信息");
           System.out.println("输入'查找教职员工'以列出有关教职员工的信息");
           System.out.println("输入'列出学生'以列出所有学生的姓名。");
           System.out.println("输入'列出教职员工'以列出教职员工的姓名");
           System.out.println("输入'退出'以结束此程序并保存数据");
      
           //-------------------->
      
           扫描器 在 = 新扫描器(System.in);
           布尔值 运行 = 真;
           当 (运行) {
               字符串 s = 在.nextLine();
               如果 (s.equals("退出")) {
                   运行 = 假;
                   // 将数据保存在外部文件中
                   尝试 {
                       FileOutputStream fos = new FileOutputStream("fileName.txt");
                       ObjectOutputStream oos = new ObjectOutputStream(fos);
                       oos.writeObject(u_city.people);
                       oos.close();
                   } 捕获 (IOException ex) {
                       ex.printStackTrace();
                   }
               }
         
               如果 (s.equals("雇佣")) {
                   u_city.hire();
               }
               如果 (s.equals("录取")) {
                   u_city.admit();
               }
               如果 (s.equals("查找学生")) {
                   System.out.println("学生的名字是什么?");
                   字符串 fn = 在.nextLine();
                   System.out.println("学生的姓氏是什么?");
                   字符串 ln = 在.nextLine();
                   学生 temp = u_city.findStudent(fn, ln);
                   如果 (temp == null) {
                       System.out.println("未找到学生");
                   } 否则 {
                       temp.Details();
                   }
               }
               如果 (s.equals("查找教职员工")) {
                   System.out.println("教职员工的名字是什么?");
                   字符串 fn = 在.nextLine();
                   System.out.println("教职员工的姓氏是什么?");
                   字符串 ln = 在.nextLine();
                   教职员工 temp = u_city.findFaculty(fn, ln);
                   如果 (temp == null) {
                       System.out.println("未找到教职员工");
                   }
               }
      
               如果 (s.equals("列出学生")) {
                   人[] temp = u_city.getStudents();
                   对于 (int i = 0; i <= temp.length; i++) {
                       System.out.println(temp[i].firstName + " " + temp[i].lastName);
                   }
             
               }
               如果 (s.equals("列出教职员工")) {
                   人[] temp = u_city.getFaculty();
                   对于 (int i = 0; i <= temp.length; i++) {
                       System.out.println(temp[i].firstName + " " + temp[i].lastName);
                   }     
               }
           }
       }
    }  
Faculty.java
    包 university_info;
    
    公共类 Faculty 扩展 Person {
       字符串[] 课程; // 教职员工教授的课程
      
       公共教职员工(String first, String last, int m, int d, int y, String[] course, 布尔值 bol) {
      
           这个.课程 = 课程; //----------->
      
           这个.firstName = first;
           这个.lastName = last;
           这个.monthBirth = m;
           这个.dayBirth = d;
           这个.yearBirth = y;
           这个.student_or_faculty = bol;
       }
      
       公共无效 详情_教职员工() {
           System.out.println("学生:" + 这个.firstName + " " + 这个.lastName);
           System.out.println("出生日期: " + 这个.dayBirth + "/" + 这个.monthBirth + "/" + 这个.yearBirth);
           System.out.println("课程: ");
           对于 (int i = 0; i <= 课程.length; i++) {
               System.out.println(课程[i]);
           }
       }
    }
Person.java
    包 university_info;
    
    公共类 Person {
       字符串 firstName;
       字符串 lastName;
       整数 monthBirth;
       整数 dayBirth;
       整数 yearBirth;
       布尔值 student_or_faculty; // 学生为真,教职员工为假
      
       公共人() {} //
<details>
<summary>英文:</summary>
I am getting this error when trying to run my program.
Can someone help me figure it out?
> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0  
> at university_info.UniversityDriver.main(UniversityDriver.java:39)
My code:
UniversityDriver.java
    package university_info;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class UniversityDriver{
    
       public static void main(String[] args) {
           // TODO Auto-generated method stub
           University u_city=new University("hero university","Teach well");
           System.out.println("Welcome to "+u_city.universityName+" Univercity");  
       System.out.println(u_city.motto);
         
      
       // retrieving data from external file on startup.....
       ArrayList<Person> al=new ArrayList<Person>();
       try{
           FileInputStream fis = new FileInputStream("fileName.txt");
           ObjectInputStream ois = new ObjectInputStream(fis);
           al = (ArrayList<Person>)ois.readObject();
           ois.close();
           }catch(FileNotFoundException ex){
               System.out.println("File not found exception");
           }catch(IOException ex){
               ex.printStackTrace();
           }catch(ClassNotFoundException ex){
              
           }
      
       Object[] array=new Object[al.size()];
       array=al.toArray(array);
      
       for(int i=0;i<=array.length;i++){
           u_city.people[i]=(Person)array[i];
       }
          
       System.out.println("What would you like to do");
       System.out.println("Enter 'hire' to hire a new faculty member. ");
       System.out.println("Enter 'admit' to admit a new student ");
       System.out.println("Enter 'find student' to list information about a student");
       System.out.println("Enter 'find faculty' to list information about a faculty member");
       System.out.println("Enter 'list students' to list the names of all students.");
       System.out.println("Enter 'list faculty' to list the names of faculty members");
       System.out.println("Enter 'quit' to end this program and save data");
      
       //-------------------->
      
      
       Scanner in =new Scanner(System.in);
       boolean run= true;
       while(run){
          
       String s= in.nextLine();
       if(s.equals("quit")){
           run=false;
           //save data in extenal file
       try{
          FileOutputStream fos = new FileOutputStream("fileName.txt");
              ObjectOutputStream oos = new ObjectOutputStream(fos);
              oos.writeObject(u_city.people);
              oos.close();
          }catch(IOException ex){
              ex.printStackTrace();
          }
       }
         
       if(s.equals("hire")){
           u_city.hire();
       }
       if(s.equals("admit")){
           u_city.admit();
       }
       if(s.equals("find_student")){
           System.out.println("What is the student's first name?");
           String fn=in.nextLine();
           System.out.println("What is the student's last name?");
           String ln=in.nextLine();
           Student temp=u_city.findStudent(fn, ln);
           if(temp==null){
               System.out.println("Student not found");
           }else{
               temp.Details();
           }
       }
       if(s.equals("find_faculty")){
           System.out.println("What is the faculty's first name?");
           String fn=in.nextLine();
           System.out.println("What is the faculty's last name?");
           String ln=in.nextLine();
           Faculty temp=u_city.findFaculty(fn, ln);
           if(temp==null){
               System.out.println("faculty not found");
           }
       }
      
       if(s.equals("list_student")){
           Person[] temp= u_city.getStudents();
           for(int i=0;i<=temp.length;i++){
               System.out.println(temp[i].firstName+" "+temp[i].lastName);
           }
             
       }
       if(s.equals("list_faculty")){
           Person[] temp= u_city.getFaculty();
           for(int i=0;i<=temp.length;i++){
               System.out.println(temp[i].firstName+" "+temp[i].lastName);
           }     
             
       }
       }
      
    }
    }  
Faculty.java
    package university_info;
    
    public class Faculty extends Person {
       String[] courses; // courses that faculty member teaches
      
       public Faculty(String first,String last,int m,int d,int y,String[] course,boolean bol){
      
       this.courses=course; //----------->
      
       this.firstName=first;
       this.lastName=last;
       this.monthBirth=m;
       this.dayBirth=d;
       this.yearBirth=y;
       this.student_or_faculty=bol;
       }
      
       public void Details_Faculty(){
           System.out.println("Student:"+ this.firstName +" "+ this.lastName);
           System.out.println("DOB: "+ this.dayBirth+"/"+this.monthBirth+"/"+this.yearBirth);
           System.out.println("Courses: ");
           for(int i=0;i<=courses.length;i++){
               System.out.println(courses[i]);
           }
       }
    }
Person.java
    package university_info;
    
    public class Person {
       String firstName;
       String lastName;
       int monthBirth;
       int dayBirth;
       int yearBirth;
       boolean student_or_faculty; // true for student and false for faculty
      
       public Person(){} //must have a constructor
    }
School.java
    package university_info;
    
    public interface School {
       Student findStudent(String fn,String ln);
       Faculty findFaculty(String fn,String ln);
      
       Faculty hire();
       Student admit();
      
       Person[] getAllPersons();
       String[] getAllMajors();
      
       String[] getAllCourses();
      
       Person[] getStudents();
       Person[] getFaculty();
    }
Student.java
    package university_info;
    
    public class Student extends Person {
       String major;
      
       public Student(String first,String last,int m,int d,int y,String major,boolean bol){
          
           this.major=major;
          
           this.firstName=first;
           this.lastName=last;
           this.monthBirth=m;
           this.dayBirth=d;
           this.yearBirth=y;
           this.student_or_faculty=bol;
       } // must have a constructor
      
       public void Details(){
           System.out.println("Student:"+ this.firstName +" "+ this.lastName);
           System.out.println("DOB: "+ this.dayBirth+"/"+this.monthBirth+"/"+this.yearBirth);
           System.out.println("Major: "+this.major);
       }
    }
University.java
    package university_info;
    
    import java.util.Scanner;
    
    public class University implements School {
      
       int MAX_SIZE= 500;
      
       String universityName;
       String motto;
       Person[] people;
       String[] majors;
       String[] courses;
      
       public University(String u_Name,String u_motto){
           this.universityName=u_Name;
           this.motto=u_motto;
          
           people=new Person[MAX_SIZE];
          
           majors=new String[4];
           majors[0]= "Hardware Architecture";
           majors[1]= "Information Analytics";
           majors[2]= "Quantum Computing";
           majors[3]= "Undecided";
          
           courses=new String[12];
           courses[0]="Computers";
           courses[1]="Advance Physics";
           courses[2]="Quantum Entanglement";
           courses[3]="Parallel Programming";
           courses[4]="Advance Algorithums";
           courses[5]="FPGA Programming";
           courses[6]="Hardware Design";
           courses[7]="Embedded Systems";
           courses[8]="Signal Processing";
           courses[9]="Artificial Intelligence";
           courses[10]="Bayesian Logic";
           courses[11]="Probablity";
       }
    
       public Student findStudent(String fn, String ln) {
           // TODO Auto-generated method stub
           for(int i=0;i<=people.length;i++){
           if(people[i].firstName.equals(fn)&& people[i].lastName.equals(ln));
           return (Student)people[i];
           }
          
           return null;
       }
    
       public Faculty findFaculty(String fn, String ln) {
           // TODO Auto-generated method stub
           for(int i=0;i<=people.length;i++){
               if(people[i].firstName.equals(fn)&& people[i].lastName.equals(ln));
               return (Faculty)people[i];
               }
          
          
           return null;
       }
    
       public Faculty hire() {
           // TODO Auto-generated method stub
          
           Scanner in =new Scanner(System.in);
           String s= in.nextLine();
           System.out.println("You entered string "+s);
          
           System.out.println("What is the person's first name?");
           String f_name= in.nextLine();
           System.out.println("What is the person's last name?");
           String l_name= in.nextLine();
           System.out.println("What is the person's month of birth?");
           int m= in.nextInt();
           System.out.println("What is the person's day of birth?");
           int d= in.nextInt();
           System.out.println("What is the person's year of birth?");
           int y= in.nextInt();
          
           String[] c=new String[20];// max 20 courses can be assigned to faculty
           int i=0;
           boolean condition=true;
           while(condition) {
               System.out.println("Assign a course to this Faculty enter 'done' if there are no other courses?");
               String course= in.nextLine();
               if(!course.equals("done")) {
                   c[i]=course;
                   i++;
               }
               condition=false;
           }
          
           boolean b=false;
           Faculty temp=new Faculty(f_name,l_name,m,d,y,c,b);      
           return temp;
       }
    
       public Student admit() {
           // TODO Auto-generated method stub
           Scanner in =new Scanner(System.in);
           String s= in.nextLine();
           System.out.println("You entered string "+s);
          
           System.out.println("What is the person's first name?");
           String f_name= in.nextLine();
           System.out.println("What is the person's last name?");
           String l_name= in.nextLine();
           System.out.println("What is the person's month of birth?");
           int m= in.nextInt();
           System.out.println("What is the person's day of birth?");
           int d= in.nextInt();
           System.out.println("What is the person's year of birth?");
           int y= in.nextInt();
           System.out.println("What is the person's major?");
           String major= in.nextLine();
          
           boolean b=true;
           Student temp=new Student(f_name,l_name,m,d,y,major,b);
           return temp;
       }
    
       public Person[] getAllPersons() {
           // TODO Auto-generated method stub
           return this.people;
       }
    
       public String[] getAllMajors() {
           // TODO Auto-generated method stub
           return this.majors;
       }
    
       public String[] getAllCourses() {
           // TODO Auto-generated method stub
           return this.courses;
       }
    
       public Person[] getStudents() {
           // TODO Auto-generated method stub
           Person[] temp=new Person[MAX_SIZE];
           for(int i=0;i<=MAX_SIZE;i++){
               if(people[i].student_or_faculty == true){
                   temp[i]=people[i];
               }
           }
          
           return temp;
       }
    
       public Person[] getFaculty() {
           // TODO Auto-generated method stub
           Person[] temp=new Person[MAX_SIZE];
           for(int i=0;i<=MAX_SIZE;i++){
               if(people[i].student_or_faculty == false){
                   temp[i]=people[i];
               }
           }
          
           return temp;
       }
    }
</details>
# 答案1
**得分**: 1
将此部分更改为:
```java
for (int i = 0; i < array.length; i++) {
    u_city.people[i] = (Person) array[i];
}
将解决此问题。循环不能超过数组的大小,而且由于索引 i 从 0 开始,最大索引号可以是数组大小减一。
希望这有所帮助。
英文:
Changing this part:
for (int i = 0; i <= array.length; i++) {
u_city.people[i] = (Person) array[i];
}
to
for (int i = 0; i < array.length; i++) {
u_city.people[i] = (Person) array[i];
}
will resolve the issue. The loop cannot exceed the size of array, and since the index i starts from 0, maximum index number could be array's size minus one.
Hope this helps out.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论