Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何实现链表和其他类_Java_Class_Linked List - Fatal编程技术网

Java 如何实现链表和其他类

Java 如何实现链表和其他类,java,class,linked-list,Java,Class,Linked List,我有一个我写的LinkedList课程,还有一个学生课程。每个学生都有一个ID、姓名、GPA 如何将这些变量作为一个元素添加到链表中。就像我需要在链表中搜索一个元素一样,我可以显示该学生的所有信息 如果您需要进一步解释,我将复制一些代码。按照java.util.List示例进行操作: List<Student> roster = new ArrayList<Student>(); 至于搜索一个给定的学生实例,我会编写一个迭代器,它可以使用一个comparator并返回链

我有一个我写的LinkedList课程,还有一个学生课程。每个学生都有一个ID、姓名、GPA

如何将这些变量作为一个元素添加到链表中。就像我需要在链表中搜索一个元素一样,我可以显示该学生的所有信息


如果您需要进一步解释,我将复制一些代码。

按照java.util.List示例进行操作:

List<Student> roster = new ArrayList<Student>();

至于搜索一个给定的学生实例,我会编写一个迭代器,它可以使用一个comparator并返回链接列表的过滤版本。

我想你有
Student
MyLinkedList
类,现在你想使用它们,因为你的链接列表可能只支持整数项。你可以用这样的东西

public class Student {
    private int id;
    private String name;
    private double gpa;
    //getters and setters...
}
现在,您需要添加
Student
类作为链接列表中节点的信息:

public class MyLinkedList {
    class MyNode {
        private Student student;
        private MyNode next;
        public MyNode(Student student) {
            this.student = student;
            this.next = null;
        }
        public Student getStudent() {
            return this.student;
        }
    }
    private MyNode root;
    private int size;
    public MyLinkedList {
        this.root = null;
    }
    public void add(Student student) {
        //this is just one way to implement the insert method
        //you can rewrite to use your own implementation
        MyNode node = new MyNode(student);
        if (root == null) {
            root = node;
        } else {
            MyNode currentNode = root;
            while (currentNode.next != null) {
                currentNode = currentNode.next;
            }
            currentNode.next = node;
        }
        size++;
    }
    public void printData() {
        //method used to print the content of the linked list
        MyNode currentNode = root;
        while (currentNode != null) {
            Student student = currentNode.getStudent();
            System.out.println("Id: " + student.getId + " Name: " + student.getName());
            currentNode = currentNode.next;
        }
    }
}
这样,您就可以使用
Student
类实现一个新的链表。让我们试试下面的代码:

public static void main(String args[]) {
    MyLinkedList mll = new MyLinkedList;
    Student student;
    student = new Student();
    student.setId(1);
    student.setName("Luiggi");
    mll.add(student);
    student = new Student();
    student.setId(2);
    student.setName("Mendoza");
    mll.add(student);
    mll.printData();
}

这只是一个示例,您可以改进代码,但您已经了解了主要思想。

请这样做,但基本上每个节点都应该有这些信息,例如节点应该有:{nextNode,id,name,gpa,…}或者如果您想要,{nextNode,element},其中一个元素可以是学生。
public static void main(String args[]) {
    MyLinkedList mll = new MyLinkedList;
    Student student;
    student = new Student();
    student.setId(1);
    student.setName("Luiggi");
    mll.add(student);
    student = new Student();
    student.setId(2);
    student.setName("Mendoza");
    mll.add(student);
    mll.printData();
}