简体   繁体   中英

How can I keep a object in linked list node?

I have a project and project wants a student management system. I started to do. Each student must have 3 properties (id,name,surname) and I have to keep a node in linked list. How can I keep that 3 properties in a node and how can I establish that connection?

Read about Inheritance, Generics in Java. You can do it like:

List<Student> students = new LinkedList<>();
students.add(new Student(1, "name1", "surname1"));
students.add(new Student(2, "name2", "surname2")); 
students.add(new Student(3, "name3", "surname3")); 

and print them like:

for (Student student : students) {
    System.out.println(student);
}

And Student class:

    public static class Student {
        private long id;
        private String name;
        private String surname;

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

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", surname='" + surname + '\'' +
                    '}';
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM