gpt4 book ai didi

java - 将多个不同位的数据存储到链表中

转载 作者:行者123 更新时间:2023-12-02 08:42:34 25 4
gpt4 key购买 nike

我需要将 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;
}

}

最佳答案

您需要创建 TeamMemberLinkedList,而不是创建 StringLinkedList,如下所示:

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;
}

确保在类 TeamMember 中添加一个 toString() 方法,以便您可以简单地执行 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() 的更多信息。

关于java - 将多个不同位的数据存储到链表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61279814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com