【问题标题】:How do I print out an array list containing both the name of a student and the id of a student?如何打印包含学生姓名和学生 ID 的数组列表?
【发布时间】:2020-02-10 01:00:47
【问题描述】:
public class pro1{
    static ArrayList <String> student = new ArrayList<String>();
    static ArrayList <Integer> id = new ArrayList<Integer>();
    String name;
    int ID;
    public pro1() {
        this.name = "";
        this.ID = 0;
    }
    public pro1(String name, int ID) {
        this.name = name;
        this.ID = ID;
    }
    public boolean addStudent(String name, int ID) {
        student.add(name);
        id.add(ID);
        return true;
    }
    /*@Override
    public String toString() {
        return name + ID;
    }*/
    public static void main(String args[]) {
        pro1 stu = new pro1();
        stu.addStudent("john", 1);
        stu.addStudent("johnny", 2);
        System.out.println(stu);
    }

}

我想使用 ArrayList 打印出学生的姓名和学生 ID。但是,我不知道该怎么做,因为在这个类中我只能打印出名称的 ArrayList 或 id 的 ArrayList。我正在考虑使用另一个类来创建一个学生对象,但我不知道该怎么做。

【问题讨论】:

标签: java arraylist


【解决方案1】:

好问题,我认为最好的解决方案是像您想的那样创建一个 Student 对象!

    public static class Student {

        private final String name;

        private final int id;

        public Student(String name, int id) {
            this.name = name;
            this.id = id;
        }

        @Override
        public String toString() {
            return String.format("name=%s, id=%s", name, id);
        }
    }

    public static class School {

        private final List<Student> students;

        public School(List<Student> students) {
            this.students = students;
        }

        public void add(Student student) {
            students.add(student);
        }

        public List<Student> getStudents() {
            return students;
        }
    }

    public static void main(String... args) {
        School school = new School(new ArrayList<>());

        school.add(new Student("jason", 1));
        school.add(new Student("jonny", 2));

        school.getStudents().forEach(System.out::println);
    }

【讨论】:

    【解决方案2】:

    非 OOP

    循环这对列表。

    第一次完整性检查:这两个列表的大小是否相同?

    if( students.size() != ids.size() ) { … Houston, we have a problem }
    

    然后循环。使用一个索引号从两个列表中提取。

    for( int index = 0 ; 
         index < students.size() ; 
         index ++ 
    ) 
    {
        System.out.println( 
            students.get( index ) + 
            " | " + 
            ids.get( index ) 
        ) ;
    }
    

    哎呀

    面向对象的方法是定义一个类。该类将有两个成员字段,即特定学生的姓名和 ID。

    然后创建一个方法来输出 String 以及您想要的输出。

    所有这些都已在 Stack Overflow 上多次介绍过。因此,搜索以了解更多信息。例如:Creating simple Student classHow to override toString() properly in Java?

    【讨论】:

      【解决方案3】:

      取消注释并修改您的toString() 方法,如下所示。它将打印学生和身份证。

      @Override
      public String toString() {
          return student.toString() + id.toString();
      }
      

      如果您想拥有 Student 到 Id 的映射,最好将它们放在 Map 集合中。 ID 作为键,学生姓名作为值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多