Java - 从类调用私有数组列表



我的模板将打开一个选项菜单,用户输入 1-3 之间的内容以选择三个选项之一。

当用户选择选项 1 时,它会要求他们输入数字teamNumber 。必须实例化类Team,然后将其写入数组列表。

如果numberList中至少有一个数字,用户可以选择选项2。它要求他们输入数组列表中的任何数字并搜索它。如果找到他们输入的数字,则输入字符串teamMemberFirstName和字符firstInitialLastName。然后它会将输入写入位于另一个类TeamMember中的私有数组列表。

在选项 1 和 2 中输入信息后,他们可以选择选项 3。它允许您根据您输入的团队编号打印输入名称的列表。

我不确定如何在选项 3 中从 TeamMember 类调用私有数组列表 teamList .关于如何继续此步骤的任何指导?我的代码如下。

主类:

public class Main {
    public static void main(String[] args) {
                        int choosing;
    Scanner scan = new Scanner(System.in);
String input;
int teamNumber;
boolean stayInLoop;
ArrayList<Team> numberList = new ArrayList<Team>();
do {
stayInLoop = true;
System.out.println("1. Add a new team");
System.out.println("2. Add a new team member");
System.out.println("3. View teams");
input = scan.nextLine();
if (input.equals("1")) {
  System.out.println("Enter a team number:");
  teamNumber = scan.nextInt();
  scan.nextLine();
  Team addTeam = new Team(teamNumber);    
  numberList.add(addTeam);
}
if (input.equals("2")){
boolean foundIt = false;
boolean valid = true;
System.out.println("Team number:");
teamNumber = scan.nextInt();
scan.nextLine();
  for (int a = 0; a < numberList.size() && foundIt == false; a++){
  Team addTeam = numberList.get(a);
    if (addTeam.findTeam() == teamNumber) {
    foundIt = true;
    System.out.println("Enter first name of team member:");
    String teamMemberFirstName = scan.nextLine();
    System.out.println("Enter first initial of last name:");
    char firstInitialLastName = scan.nextLine().charAt(0);
    TeamMember inputTeamMember = new TeamMember(teamMemberFirstName, firstInitialLastName);
    inputTeamMember.addMember(inputTeamMember, valid = true);
    System.out.println("Success!");
    }
  }
  if (foundIt == false) {
  System.out.println("Try again.");
  }
}
if (input.equals("3")){
  for (int a = 0; a < numberList.size(); a++) {
  Team addTeam = numberList.get(a);
  //Not sure what to put where there are ????'s - I tried a few ideas and stuff I found online, but nothing worked    
  //I assume I call the method/class here????
  System.out.println("Team: " + addTeam.findTeam() + " Members: " + 
  "I will put the member called from the arraylist here????"); 
  }
}
}while (stayInLoop == true;)
}}

团队成员类:

public class TeamMember {
private final String teamMemberFirstName;
private final char firstInitialLastName;
private ArrayList<TeamMember> teamList = new ArrayList<>();
public TeamMember(String teamMemberFirstName, char firstInitialLastName) {
  this.teamMemberFirstName = teamMemberFirstName;
  this.firstInitialLastName = firstInitialLastName;
}
public int addMember(TeamMember member, boolean valid) {
  valid = teamList.add(member);
  return teamList.size();
}
}

您不能直接从其他类访问私有字段。将列表移动到 Team 类或创建一个 getter 来检索列表。

在公共类中,可以在公共方法中返回private对象。这似乎是这个项目中最简单的方法。向TeamMember类添加一个新方法,并让它返回teamList

在 TeamMember 类中,分配私有变量后的任何位置

public static ArrayList show(){ 
//the static keyword, in short, will make the method callable without a class instance.
    return teamList;
}

由于 TeamMember 方法show()现在是静态的,因此您应该能够简单地调用 TeamMember.show() 并获取 ArrayList。

重要说明:为了使它正常工作,您还必须将私有数组列表设置为静态。静态对象不能调用非静态对象。

这将把它变成private static ArrayList<TeamMember> teamList = new ArrayList<>();

在主类中,就像我上面说的,只需调用TeamMember.show()。您无需创建实例。

如果将

teamList更改为public而不是privateMain类将能够访问该变量。当你在 Java 中private某些东西时,你基本上是使该实例变量只能通过实例化的类访问。如果您希望变量对其他类可见以供参考,则应使其public

由于赋值需要它,因此您需要为"teamList"变量定义一个getter和setter。

public void setArray(ArrayList newTeamList){
    teamList = newTeamList;
}
public ArrayList getArray(){
    return teamList;
}

这将允许您通过以下方法访问私有变量

最新更新