如何从链接列表中打印元素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从链接列表中打印元素相关的知识,希望对你有一定的参考价值。

我有一个带有控制器类和作为主要类的测试类的链表类。该代码将运行并接受用户输入,但是当我单击以显示所有输入的团队成员时,它为空。我哪里出问题了?如何让所有团队成员都进入显示状态?

public class TeamMember 


private LinkedList<TeamMember> teamMembers;

public LinkedList<TeamMember> getTeamMembers() 
    return teamMembers;


public void setTeamMembers(LinkedList<TeamMember> teamMembers) 
    this.teamMembers = teamMembers;



private Scanner scan = new Scanner(System.in);

public TeamMember(String name) 
    this.teamMembers = new LinkedList<>();







package com.view;


import com.controller.TeamMemberController;
import com.model.TeamMember;

import java.util.InputMismatchException;
import java.util.Scanner;

public class TeamMemberTest 

public static void main(String[] args) 

    Scanner scan = new Scanner(System.in);
    TeamMemberController teamMemberController = new TeamMemberController();
    int userInput;

    do 

        System.out.println("1. Add a new team member");
        System.out.println("2. Find and display a team member");
        System.out.println("3. Remove a team member");
        System.out.println("4. Display all team members");
        System.out.println("0. Quit");

        //Validate user input
        try 
            userInput = scan.nextInt();
            scan.nextLine();
         catch (InputMismatchException e) 

            //if anything other than an integer is entered.
            //The "scan.nextLine" fix above will not be triggered.
            //It has to appear in the catch as well.
            scan.nextLine();
            userInput = 5;
        

        switch (userInput) 
            case 0:
                userInput = teamMemberController.quit();
                break;

            case 1:
                System.out.println("**********\n" + teamMemberController.addTeamMember());
                break;
            case 2:

                System.out.println("**********\n" + teamMemberController.findTeamMember());
                break;
            case 3:
                System.out.println("**********\n" + teamMemberController.removeTeamMember());
                break;
            case 4:
                teamMemberController.displayAllTeamMembers();
                break;
            default:
                System.out.println("*** Please Make another selection ***");
                System.out.println("");
                break;
        
     while (userInput != 0);







package com.controller;

import com.model.TeamMember;
import java.util.LinkedList;
import java.util.Scanner;


public class TeamMemberController 
private TeamMember teamMember;

public TeamMemberController() 
    String name = null;
    this.teamMember = new TeamMember(name);


Scanner scan = new Scanner(System.in);

public String addTeamMember() 
    System.out.println("To go back press 0");
    String name = null;

    boolean keepLooping = true;

    //get team member linked list
    LinkedList<TeamMember> teamMembers = teamMember.getTeamMembers();


    //user enters name
    while (keepLooping) 
        System.out.println("Enter team member name");
        //project names MUST be UNIQUE
        name = scan.nextLine();
        if (name.equals("0")) 
            return "team member not added";
        

        if (!this.checkIfTeamMemberNameExists(name)) 
            keepLooping = false;
         else 
            System.out.println("Team Member already exists");
            System.out.println("");
        
    



    //add team member to collection
    teamMembers.add(new TeamMember(name));

    teamMember.setTeamMembers(teamMembers);

    //returns true when a team member has been successfully added
    return "Name: " + name + "\n--Added--";


public String findTeamMember() 

    LinkedList<TeamMember> teamMembers = teamMember.getTeamMembers();

    //if the company has no team members no need to continue
    if (teamMembers.isEmpty()) 
        return "Sorry no team members";
    

    //get here company must have team members
    System.out.println("Enter team member name");
    String name = scan.nextLine();


    for (TeamMember t : teamMembers) 
        if (t.getTeamMembers().equals(name)) 
            return "Name: " + t.getTeamMembers();
        
    
    return "Team member not found";


public String removeTeamMember() 

    LinkedList<TeamMember> teamMembers = teamMember.getTeamMembers();
    System.out.println("Enter name of team member to remove");
    System.out.println("Enter 0 to exit");
    String projectToRemove = scan.nextLine();
    boolean removed = false;

    if (projectToRemove.equals("0")) 
        return "No project removed";
    

    //check existing team member names against the user input one
    for (TeamMember t : teamMembers) 
        if (t.getTeamMembers().equals(removeTeamMember())) 
            teamMembers.remove(t);
            removed = true;
        
    
    if (removed) 
        teamMember.setTeamMembers(teamMembers);
     else
        return "No team member found";

    return removeTeamMember() + " has successfully been removed";


public void displayAllTeamMembers() 

    LinkedList<TeamMember> teamMembers = teamMember.getTeamMembers();
    if (teamMembers.isEmpty()) 
        System.out.println("Company has not added any team members");
        return;
    


    System.out.format(" Name%n");

    for (TeamMember t : teamMembers) 


        System.out.println(t.getTeamMembers() + "         ");
    




public int quit() 
    System.out.println("Are you sure you want to quit? y/n");
    String userResponse = scan.nextLine();
    boolean loop = true;

    while (loop) 

        if (userResponse.equalsIgnoreCase("y")) 
            System.out.println("Program ending");
            return 0;
         else if (userResponse.equalsIgnoreCase("n"))
            return 5;
    

    return 0;




public boolean checkIfTeamMemberNameExists(String name) 

    //get team member linked list
    LinkedList<TeamMember> teamMembers = teamMember.getTeamMembers();

    //if a team member with the same name already exists return true
    if (!teamMembers.isEmpty()) 
        for (TeamMember t : teamMembers) 
            if (t.getTeamMembers().equals(name)) 
                return true;
            
        
    


    return false;




这是我得到的输出。如何打印输入的名称?

[1]: https://i.stack.imgur.com/FVhVm.png

答案

以下是使代码正常工作所需的最小代码更改。但是,如果您围绕此代码构建一个可以工作的产品就绪系统,我建议您遵循设计模式。

公共类TeamMember

private static LinkedList<TeamMember> teamMembers;

public LinkedList<TeamMember> getTeamMembers() 
    return teamMembers;


public void setTeamMembers(LinkedList<TeamMember> teamMembers) 
    teamMembers = teamMembers;



private Scanner scan = new Scanner(System.in);

public TeamMember(String name) 
    if(teamMembers==null) 
        teamMembers = new LinkedList<>();
    

另一答案

第一个问题是,TeamMembers不记得您给他们提供的名称。添加名称字段和检索方法:

private String name;
public TeamMember(String name) 
    this.name = name;
    this.teamMembers = new LinkedList<>();

public String getName() 
    return name;

第二个问题是displayAllTeamMembers不显示团队成员的“姓名”。不可能做到这一点,因为团队成员甚至都没有名字,但现在他们有了。

public void displayAllTeamMembers() 

    LinkedList<TeamMember> teamMembers = teamMember.getTeamMembers();
    if (teamMembers.isEmpty()) 
        System.out.println("Company has not added any team members");
        return;
    

    System.out.format(" Name%n");

    for (TeamMember t : teamMembers) 
        System.out.println(t.getName());
    

第三个可能的问题是,每个“团队成员”都有一个团队成员列表。这似乎让我感到困惑,让我想知道您是否误读了此练习的说明。

以上是关于如何从链接列表中打印元素的主要内容,如果未能解决你的问题,请参考以下文章

如何使用变量从列表中打印特定元素?

如何从自定义列表视图中获取选定项目并在 toast 消息中打印?

如何打印列表中具有起始字母“A”的元素[重复]

结束一个片段

如何将列表视图中的数据从一个片段发送到另一个片段

如何打印列表的单个元素?