我需要将3个不同的数据位存储到一个链表中。第一个是我工作的名字,然后我需要存储员工编号及其职业。我找不到有关将节点链接到一组数据的任何信息。任何帮助将不胜感激。

这是TeamMemberTest运行的代码:

package teammember;

import java.util.*;
import java.util.Scanner;

public class TeamMemberTest {
    public static void main(String args[]) {
    /* Linked List Declaration */
    LinkedList<String> linkedlist = new LinkedList<String>();

    Scanner input = new Scanner(System.in);
    boolean mainloop = true;
    String name;
    int employeeNo;
    String position;
    int choice;
    while(true){
        System.out.println("Menu");
        System.out.print("1. add project \n");
        System.out.print("2. display project \n");
        System.out.print("3. remove project \n");
        System.out.print("4. display all projects \n");
        System.out.print("\nEnter your option: ");

        choice = input.nextInt();

        switch(choice){
            case 1:
                /*add(String Element) is used for adding
                * the elements to the linked list*/
                name = Input.getString("name: ");
                employeeNo = Input.getInteger("Enter employee number: ");
                position = Input.getString("Enter employee position: ");
                linkedlist.add(name);
                System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
                break;
            case 2:
                name = Input.getString("name: ");
                if(linkedlist.contains(name)){
                    System.out.println("LinkedList does contain " + name);
                }
                break;
            case 3:
                name = Input.getString("name: ");
                linkedlist.remove(name);
                System.out.println("LinkedList after deletion of first and last element: " +linkedlist);
                break;
            case 4:
                System.out.println("Linked List Content: " +linkedlist);
                break;
            default :
                System.out.println("This is not a valid option try again.");
                break;
            }
        }
    }
}


下面的代码是TeamMember.java:

package teammember;

public class TeamMember {
    //variables
    private String name;
    private int employeeNo;
    private String position;

    //the default constant
    public TeamMember(){
        name = " ";
        employeeNo = 0;
        position = " ";
    }
    //overload the constructor
    public TeamMember(String name, int employeeNo, String position){
        this.name = name;
        this.employeeNo = employeeNo;
        this.position = position;
    }
    //the methods
    public void setName(String name){
        this.name = name;
    }
    public void setemployeeNo(int employeeNo){
        this.employeeNo = employeeNo;
    }
    public void setPosition(String position){
        this.position = position;
    }
    public String getName(){
        return name;
    }
    public int getemployeeNo(){
        return employeeNo;
    }
    public String getPosition(){
        return position;
    }
}


这是输入代码:

package teammember;

import java.io.*;
public class Input{
    private static BufferedReader input=new BufferedReader(new InputStreamReader(System.in));

    public static Character getCharacter(String prompt){
        Character value;
        System.out.print(prompt);
        try{
            value=Input.input.readLine().charAt(0);
        }
        catch(Exception error){
            // error condition
            value=null;
        }
        return value;
    }

public static Double getDouble(String prompt){
    Double value;
    System.out.print(prompt);
    try{
        value=Double.parseDouble(Input.input.readLine());
    }
    catch(Exception error){
        // error condition
        throw new NumberFormatException();
    }
    return value;
}

public static Integer getInteger(String prompt){
    Integer value;
    System.out.print(prompt);
    try{
        value=Integer.parseInt(Input.input.readLine());
    }
    catch(Exception error){
        // error condition
        throw new NumberFormatException();
    }
    return value;
}

public static String getString(String prompt){
    String string;
    System.out.print(prompt);
    try{
        string=Input.input.readLine();
    }
    catch(Exception error){
        // error condition
        string=null;
    }
    return string;
}


}

最佳答案

代替创建LinkedListString,您需要创建LinkedListTeamMember,如下所示:

LinkedList<TeamMember> linkedlist = new LinkedList<TeamMember>();


然后您需要相应地更改案例,例如

choice = input.nextInt();
boolean found;
switch (choice) {
case 1:
    name = Input.getString("name: ");
    employeeNo = Input.getInteger("Enter employee number: ");
    position = Input.getString("Enter employee position: ");
    linkedlist.add(new TeamMember(name, employeeNo, position));
    break;

case 2:
    found = false;
    name = Input.getString("name: ");
    for (TeamMember teamMember : linkedlist) {
        if (teamMember.getName().equalsIgnoreCase(name)) {
            System.out.println("Name: " + teamMember.getName() + "Employee No.: "
                    + teamMember.getemployeeNo() + "Position: " + teamMember.getPosition());
            found = true;
            break;
        }
    }
    if (!found) {
        System.out.println("The team member was not found.");
    }
    break;

case 3:
    found = false;
    name = Input.getString("name: ");
    for (TeamMember teamMember : linkedlist) {
        if (teamMember.getName().equalsIgnoreCase(name)) {
            linkedlist.remove(teamMember);
            found = true;
            break;
        }
    }
    if (found) {
        System.out.println("LinkedList after deletion of " + name + "'s record" + ": " + linkedlist);
    } else {
        System.out.println("The team member was not found.");
    }
    break;

case 4:
    System.out.println("Linked List Content: " + linkedlist);
    break;

default:
    System.out.println("This is not a valid option try again.");
    break;
}


确保在类toString()中添加一个TeamMember方法,以便您可以像System.out.println(an-object-of-TeamMember);那样简单地进行操作。您的IDE可以在单击按钮时生成toString()方法。下面给出的是eclipse IDE为您的类自动生成的toString()方法:

@Override
public String toString() {
    return "TeamMember [name=" + name + ", employeeNo=" + employeeNo + ", position=" + position + "]";
}


选中this以了解有关toString()的更多信息。

09-03 17:52