Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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_Linked List - Fatal编程技术网

Java 链表学生管理系统

Java 链表学生管理系统,java,linked-list,Java,Linked List,程序的作用:我有一个程序,应该给用户4个选项…添加学生,删除学生,打印学生信息,然后退出。如果用户输入1,则会选择添加学生,并询问您要添加多少学生。然后,他们将输入需要添加的数量,并为每个添加的内容填写学生班级所需的所有内容。如果选择选项2,则将按名称从链接列表中删除一名学生。如果选择选项3,则只打印学生姓名、邮编和专业(其他项目将在不同项目中使用)。如果选择选项4,它将退出。它将一直显示此菜单,直到选择4为止 我的问题是:我需要帮助将一个学生添加到链接列表中,并按该学生的姓名将其删除。阿尔索

程序的作用:我有一个程序,应该给用户4个选项…添加学生,删除学生,打印学生信息,然后退出。如果用户输入1,则会选择添加学生,并询问您要添加多少学生。然后,他们将输入需要添加的数量,并为每个添加的内容填写学生班级所需的所有内容。如果选择选项2,则将按名称从链接列表中删除一名学生。如果选择选项3,则只打印学生姓名、邮编和专业(其他项目将在不同项目中使用)。如果选择选项4,它将退出。它将一直显示此菜单,直到选择4为止

我的问题是:我需要帮助将一个学生添加到链接列表中,并按该学生的姓名将其删除。阿尔索 如果有人能向我解释一下toString(我的老师让我这样做),我将不胜感激

学生班级:

 public class Student {
private int studentID;
private String fName;
private String lName;
private String Address;
private String city;
private String state;
private String zip;
private String major;


public void Student(){

}

public void Student(int i, String f, String l, String a, String c, String s, String z, String m){
    studentID = i;
    fName = f;
    lName = l;
    Address = a;
    city = c;
    state = s;
    zip = z;
    major = m;
}

public int getStudentID(){
    return studentID;
}
public void setStudentID(int i){
    studentID = i;
}
public String getFName(){
    return fName;
}
public void setFName(String f){
    fName = f;
}
public String getLName(){
    return lName;
}
public void setLName(String l){
    lName = l;
}
public String getAddress(){
    return Address;
}
public void setAddress(String a){
    Address = a;
}
public String getCity(){
    return city;
}
public void setCity(String c){
    city = c;
}
public String getState(){
    return state;
}
public void setState(String s){
    state = s;
}
public String getZip(){
    return zip;
}
public void setZip(String z){
    zip = z;;
}
public String getMajor(){
    return major;
}
public void setMajor(String m){
    major = m;
}

}
链表类别:

public class LinkedList {

private class Node{
    String value;
    Node next;

    Node (String val, Node n){
         value=val;
        next = n;
    }

    Node (String val)
    {
        this(val, null);

    }
}
private Node first;
private Node last;

public LinkedList(){
    first = null;
    last = null;
}

public boolean isEmpty(){
    return first== null;
}

public int size(){
    int count = 0;
    Node p = first;
    while (p !=null){
        count++;
        p = p.next;
    }
    return count;
}

public void add( String s){
    if (isEmpty())
    {
        first = new Node(s);
        last = first;
    }
    else 
    {
        last.next = new Node(s);
        last = last.next;

    }
}

public void  add(int index, String s){
    if (index <0 || index > size()){
        String message = String.valueOf(index);
        throw new IndexOutOfBoundsException(message);
    }


    if (index ==0){
        first = new Node(s, first);
        if (last == null)
            last = first;
        return;
    }

    Node pred = first;
    for (int k = 1; k <= index -1; k++){
        pred = pred.next;
    }
    pred.next = new Node (s, pred.next);

    if (pred.next.next == null){
        last = pred.next;
    }
}
public String toString(){
StringBuilder strBuilder = new StringBuilder();

Node p = first;
while (p != null){
    strBuilder.append(p.value+"\n");
    p = p.next;

    }
    return strBuilder.toString();
}

public String remove(int index){
    if (index <0 || index >=size()){
        String message = String.valueOf(index);
        throw new IndexOutOfBoundsException(message);
    }

    String element;
    if (index == 0){
        element = first.value;
        first = first.next;
        if (first == null){
            last = null;
        }
    }
    else 
    {
        Node pred = first;
        for (int k = 1; k <=index -1; k++)
            pred = pred.next;


        element = pred.next.value;
        pred.next = pred.next.next;
        if (pred.next ==  null)
            last = pred;

    }
    return element;
}




}

从我的理解来看,你似乎并不关心学生在链表中的顺序。因此,您可以有一种添加方法,例如:

// This is the start of your linked list
Node start = ...;

public void add(String s){
    // This may change depending on how you construct a node
    Node toAdd = new Node(s);
    // Iterate until we reach the end
    while(start.getNextNode() != null)
        start = start.getNextNode();
    start.setNextNode(toAdd);

}
虽然您可能没有我上面添加的一些方法,但最好实现它们

要删除,您可以使用以下内容:

// This is the start of your linked list
Node start = ...;

public void remove(String s){
    // Iterate until we find the node who's next one is the one to delete
    // We're assuming toString() returns the name of the student
    while(start.getNextNode().toString() != s)
        start = start.getNextNode();
    // Set our next node to the one 2 nodes down the line, thus skipping it
    start.setNextNode(start.getNextNode().getNextNode())

}
现在您要问的是什么是toString方法,基本上是一个toString()方法,它返回一个提供对象信息的字符串。 Student类可能的toString()方法可以是

public String toString(){
    return "Hello! My name is " + fname + .....; // so on and so on
}
请注意,Java中的每个对象都有一个toString方法,因此编写自己的方法将覆盖其超类的方法。如果不使用toString()扩展一个类,那么它将返回到它能找到的任何一个toString,其中最远的是它在内存存储中的位置的十六进制代码(我可能对此有错误,如果没有,请纠正我)


希望这有帮助

请自己做作业,首先自己尝试,然后如果你遇到任何问题,请在这里询问。
public String toString(){
    return "Hello! My name is " + fname + .....; // so on and so on
}