调用方法在另一个类中创建数组

时间:2014-12-05 01:35:59

标签: java arrays methods arraylist

StudentDemo:

public class studentDemo {
    public static void main(String[] args) throws IOException{
        int runit = 0;

        StudentMenu run = new StudentMenu();
        while (runit != 1) {
            run.menu();
        }
    }
}

StudentMenu:

public class StudentMenu {

    public Student func = new Student();
    List<Student> studentList = new ArrayList<Student>();

    String input;
    String input2;
    String input3;

    public void menu(){
        System.out.println("Choose an option: ");
        System.out.println("1) Add a student ");
        System.out.println("2) Display student list ");
        System.out.println("3) Exit ");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {

            input = br.readLine();
            int choice = Integer.parseInt(input);
            System.out.println("Choice: " + choice);

            if(choice == 1){
                System.out.println("First name: ");
                input = br.readLine();
                System.out.println("Last name: ");
                input2 = br.readLine();
                System.out.println("Number of classes to add for student: ");
                input3 = br.readLine();
                int numberOfClasses = Integer.parseInt(input3);

                studentList.add(new Student(input, input2, numberOfClasses));

                func.createClassList(numberOfClasses);
            }

            if(choice == 2){
            }

            if(choice == 3){
                System.exit(0);
            }       
        }
        catch (IOException ioe){ 
            System.out.println("An unexpected error occured.");
        }
    }
}

学生:

public class Student{   

    private String firstName;
    private String lastName;
    private int classes;
    //Create array for classes
    private String[] classesList = new String[classes];

    public Student(){
        firstName = "no name";
        lastName = "no name";
        classes = 0;
    }

    //overloaded constructor
    public Student(String theFirstName, String theLastName, int theClasses){

        firstName = theFirstName;
        lastName = theLastName;
        classes = theClasses;
        System.out.println("Number of Classes: " + classes);
    }

    //Populating array
    public void createClassList(int classes){

        //Call array method
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try{    
            for(int i = 0; i < classes; i++){
                System.out.println("Number of Classes: " + classes);
                System.out.println("Enter class " + i + ":");
                String input = br.readLine();
                classesList[i] = input;
            }
        }
        catch (IOException ioe) { 
            System.out.println("An unexpected error occured.");
        }
    }

    //Getters
    public String getFirstName(){ return firstName; }

    public String getLasName(){ return lastName; }

    public int getNumberOfClasses(){ return classes; }

    //Setters
    public void setFirstName(String newFirstName){
        firstName = newFirstName;
    }

    public void setLastName(String newLastName){
        firstName = newLastName;
    }

    public void setNumberOfClasses(int newNumberOfClasses){
        classes = newNumberOfClasses;
    }
}

很抱歉,如果我错误地粘贴了这个。

我正在尝试创建一个程序,用他们的名字,姓氏和类列表构建学生列表。但是,在进入第一堂课后我一直遇到这个错误:

ABRT problem creation: 'success'
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Student.createClassList(Student.java:37)
at StudentMenu.menu(StudentMenu.java:37)
at studentDemo.main(studentDemo.java:10)

这是错误之前的输出:

Choose an option: 
1) Add a student 
2) Display student list 
3) Exit 
1
Choice: 1
First name: 
t
Last name: 
s
Number of classes to add for student: 
3
Enter class 0:
eng

输入eng后,错误就出现了。
我所知道的不应该有OutOfBoundsException,因为3是索引的限制,我只在一个类中输入。

非常感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:2)

此:

private int classes; // default value of 0

private String[] classesList = new String[classes];

创建一个长度为0的String数组,这对你没有多大用处。将classesList数组声明为您正在执行的字段,但仅为分配一个新数组设置具有可行的非0 int值的类。

答案 1 :(得分:0)

private String[] classesList = null;

//overloaded constructor
public Student(String theFirstName, String theLastName, int theClasses)
{
    firstName = theFirstName;
    lastName = theLastName;
    classes = theClasses;

    classesList = new String[classes]; // THIS

    System.out.println("Number of Classes: " + classes);
}

试试呢?

您的代码的另一个问题:

public Student func = new Student();
List<Student> studentList = new ArrayList<Student>();

studentList.add(new Student(input, input2, numberOfClasses));
func.createClassList(numberOfClasses);

正如您应该注意的那样,func是使用默认构造函数创建的 您的Student类中的classes变量不会使用任何正确的值进行初始化,并且您的Student类中的String []将无法正确创建!
您正在func上调用 createClassList ,这是使用默认构造函数创建的,而不是来自用户输入的 numberOfClasses 值。

  

阅读我所说的,WaterlessStraw! :)

您可能想要做的事情:

public Student func = null;
List<Student> studentList = new ArrayList<Student>();

func = new Student(input, input2, numberOfClasses);
studentList.add(func);
func.createClassList(numberOfClasses);