打印数组列表中的元素

时间:2018-12-09 22:05:02

标签: java arrays arraylist methods

我正在尝试获得与此类似的输出-

  

John Smith已添加到学生列表中

     

汤姆·威尔(Tom Will)已添加到学生列表中

     

您的姓名已添加到学生列表中

     

-开始-

     

姓名:John Smith电子邮件:js@qmul.ac.uk年份:2008

     

姓名:Tom Will电子邮件:tw@qmul.ac.uk年份:2007

     

姓名:您的姓名电子邮件:您的电子邮件年份:您的年份

     

-结束-

     

汤姆·威尔(Tom Will)已从学生列表中删除

     

-开始-

     

姓名:John Smith电子邮件:js@qmul.ac.uk年份:2008

     

姓名:您的姓名电子邮件:您的电子邮件年份:您的年份

     

-结束-

但是它正在打印时弄糊涂了。我的代码哪里出问题了?

代表学生的班级:

public class Student { 

    private String name;
    private String email;
    private int year; //year of registration to the course

    /**
     *  Constructor
     *
     *@param  name, email and year of registration
     */
    public Student(String name, String email, int year){
        this.name = name;
        this.email = email;
        this.year = year;
    }

    /**
     *  get the name
     *
     *@return the name
     */
    public String getName(){
        return name;
    }


    /**
     *  A toString() method to give a String representation of a Student
     *
     *@return    The String representation of a Student
     */
    public String toString(){
        return "Name:" + name +" Email:" + email + " Year:" + year;
    }

}

public class StudentList {

    private ArrayList<Student> list; //instance variable

    /**
     *  Constructor
     */
    public StudentList(){
        list = new ArrayList<Student>();
    }

    /**
     *  a method to print off all ArrayList elements
     */

    public void printList(){
        System.out.println("--Begin--");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
            }       
        System.out.println("--End--");

    }

    /**
     *  A method to add a student to the list
     *
     *@param    The student
     */
    public void addToList(Student s){
        list.add(s);

        System.out.println("--Begin--");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i) +"has been addded to the list");
            }
        System.out.println("--End--");

    }

    /**
     *  A method to remove a student from the list
     *
     *@param    The student
     */
    public void removeFromList(Student s){
        list.remove(s);
        System.out.println("--Begin--");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i) +"has been removed from the list");
        }
        System.out.println("--End--");

    }

    /**
     *  A main method to test
     */
    public static void main(String[] args) {

        // Create an instance of the class 
        StudentList studentList = new StudentList();

        //create 3 student objects
        Student s1 = new Student("John Smith", "js@qmul.ac.uk", 2008);
        Student s2 = new Student("Tom Will", "tw@qmul.ac.uk", 2007);
        Student s3 =  new Student("Cameron Young","Cammyoung@live.co.uk",2018);

        //add the three students to the list
        studentList.addToList(s1);
        studentList.addToList(s2);
        studentList.addToList(s3);

        // Print the list
        studentList.printList();

        // Remove the student "Tom Will"
        studentList.removeFromList(s2);

        // Print the list again
        studentList.printList();
    }

}

2 个答案:

答案 0 :(得分:2)

每次添加或删除用户时,您都在打印整个列表。我认为这就是您所说的“糊涂”。

您的addToList方法应如下所示:

public void addToList(Student s) {
    list.add(s);    
    System.out.println(s.getName() + " has been addded to the list");           
}

答案 1 :(得分:1)

在addToList和removeFromList中,您打印集合中的所有学生。您可以删除循环并仅使用参数。并使用getName()仅打印名称