该程序的作用:所以我有一个程序应该给用户4个选项...添加学生,删除学生,打印学生信息,然后退出。如果用户输入1,则将选择添加学生,并且它将询问您要添加多少学生。然后他们将输入他们需要添加的数量,他们将填写每个添加的学生班级所需的一切。如果选择了选项2,则它将按名称从链接列表中删除学生。如果选择了选项3,那么它将只打印学生姓名,zip和专业(其他项目将用于不同的项目)。如果选择选项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;
}
}
Main(到目前为止真的很糟糕):
import java.util.Scanner;
public class StudentDemo {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
LinkedList list = new LinkedList();
menu();
int input = key.nextInt();
if (input == 1){
if (input ==1){
System.out.println("Enter name");
String name = key.nextLine();
list.add(name);
System.out.println(list);
}
}
}
public static void menu(){
System.out.println("Student Maintenence System:");
System.out.println("1. Add Student");
System.out.println("2. Remove Student");
System.out.println("3. Print Student Information");
System.out.println("4. Exit");
}
public static void optionOne(int input, LinkedList list, Scanner key){
if (input ==1){
System.out.println("Enter name");/*supposed to ask for a lot more but for the time being its only asking this*/
String name = key.nextLine();
list.add(name);
list.toString();
}
}
}
答案 0 :(得分:0)
从我的解释来看,您似乎并不关心链表中学生的顺序。因此,你可以有一个添加方法,如:
// 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,其中最远的是它在内存存储中的位置的十六进制代码(我可能错了所以纠正我,如果不是)
希望这有帮助!