双向链表添加空值

Doubly Linked List adding null

我是 Java 的新手。我想制作学生信息系统,但是每当我使用 addStudent 方法时,我都会得到一个包含空值的列表。

这是学生class,用于获取姓名电话号码和学号。

import java.util.ArrayList;

public class Student {
    private String adSoyad;
    private int ogrenciNo;
    private String telefonNo;

    public Student() {
        adSoyad = null;
        ogrenciNo = 0;
        telefonNo = null;
    }
    public Student(int ogrenciNo,String adSoyad,String telefonNo){
        this.adSoyad = adSoyad;
        this.ogrenciNo = ogrenciNo;
        this.telefonNo = telefonNo;
    }

    public Student(Student student){
        this.adSoyad = student.adSoyad;
        this.ogrenciNo = student.ogrenciNo;
        this.telefonNo = student.telefonNo;
    }

    public void setAdSoyad(String adSoyad) {
        this.adSoyad = adSoyad;
    }
    public void setOgrenciNo(int ogrenciNo) {
        this.ogrenciNo = ogrenciNo;
    }
    public void setTelefonNo(String telefonNo) {
        this.telefonNo = telefonNo;
    }

    public String getAdSoyad() {
        return adSoyad;
    }
    public int getOgrenciNo() {
        return ogrenciNo;
    }
    public String getTelefonNo() {
        return telefonNo;
    }


    @Override
    public String toString() {
        return (ogrenciNo+","+adSoyad+","+telefonNo);
    }
}

这是节点 class。方法可能是错误的,也许它们太复杂了,对此深表歉意。我正在努力学习 Java.

public class Node {
    String line;
    Node next;
    Node prev;
    private Student student;

    public Node(Student Student)
    {
        this.student = Student;
    }

    public Node(String line) {
        this.line = line;
    }

    public void setLine(String line) {
        this.line = line;

    }

    public void setBilgiler(Student bilgiler) {
        this.student = bilgiler;
    }

    public void setNext(Node node) {
        next = node;
    }

    public void setPrev(Node node) {
        prev = node;
    }

    public Student getBilgiler() {
        return student;
    }

    public Node getNext() {
        return next;
    }

    public Node getPrev() {
        return prev;
    }

    public String getLine() {
        return line;
    }
}

这是双向链表。我认为我的主要问题就在这里,但我没有弄清楚如何解决这个问题,因此不胜感激。

public class DoublyLinkedList {
    private Node head;
    private Node tail;

    public DoublyLinkedList() {
        this.head = null;
        this.tail = null;
    }

    public void addLine(String line) {
        Node newNode = new Node(line);

        if (head == null) {
            head = tail = newNode;
            head.prev = null;
        } else {
            head.prev = newNode;
            newNode.next = head;
            newNode.prev = null;
            head = newNode;
        }
    }

    public void addStudent(Student student) {
        Node newNode = new Node(student);

        if (head == null) {
            head = tail = newNode;
            head.prev = null;
        } else {
            head.prev = newNode;
            newNode.next = head;
            newNode.prev = null;
            head = newNode;
        }
    }

    public void display() {
        Node current = head;
        if (head == null) {
            System.out.println("CHECK AGAIN PLEASE!!");
        }
        while (current != null) {
            System.out.print(current.line + "\n");
            current = current.next;
        }
    }

}
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        DoublyLinkedList liste = new DoublyLinkedList();

        DoublyLinkedList liste2 = new DoublyLinkedList();

        File file = new File("C:\Users\user1\OneDrive\ogrenciler.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String st;
        while ((st = br.readLine()) != null) {
            liste.addLine(st);
        }

        Student student1 = new Student(1234, "John Rick", "+1 111-111-1111");
        Student obj = new Student();
        obj.telefonNolar.add(student1.getOgrenciNo() + student1.getTelefonNo());

        liste2.addStudent(student1);

        liste.display();
        liste2.display();

    }
}

无论您是否设置了“line”成员,您的“显示”函数都只打印节点的“line”成员。 并且您的“liste2”变量填充有“Student”实例而不是“String”。这意味着只会设置节点的“student”成员,而“line”成员将保留为“null”。 因此打印第二个列表时,您只会看到空值,因为从未为节点设置“行”成员。