[英]Doubly Linked List adding null
我是 Java 新手。 我想制作學生信息系統,但是每當我使用addStudent
方法時,我都會得到一個包含空值的列表。
這是用於獲取姓名電話號碼和學生 ID 的學生類。
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);
}
}
這是節點類。 方法可能是錯誤的,也許它們太復雜了,抱歉。 我正在努力學習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();
}
}
您的“顯示”功能僅打印節點的“線”成員,無論您是否設置“線”成員。 並且您的“liste2”變量填充了“學生”實例而不是“字符串”。 這意味着只會設置節點的“student”成員,而“line”成員將保留為“null”。 因此打印第二個列表,您只會看到空值,因為從未為節點設置“行”成員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.