【问题标题】:how to add object in the null array with checking existing array如何通过检查现有数组在空数组中添加对象
【发布时间】:2020-04-21 04:16:48
【问题描述】:

我是一名 java 初学者,我尝试将播放器添加到数组中。我以为我成功地处理了这个问题,但事实并非如此。我做了以下事情:

  1. 我有一个拆分文本发送来创建一个播放器。
  2. 我在 NimPlayer 中创建了一个构造函数。
  3. 我在 Nimsys 中设置了数组来保存创建的玩家数据。

我有两个问题:

  1. 可以肯定的是,数组一开始都是空的。如果我在这里使用唯一的数组,是否可以在这里添加播放器?
  2. 我已设置counter 用于录制创建的播放器,但我无法在 else 语句中执行此操作。

这是我的代码:

int static counter;
public static String[] splitName(String inName) {
String[] splittedLine = inName.split(",");
String[] name = null;
if (splittedLine.length==3) {
    String userName = splittedLine[0].trim();
    String familyName = splittedLine[1].trim();
    String givenName = splittedLine[2].trim();
    name = new String[3];
    name[0] = userName;
    name[1] = familyName;
    name[2] = givenName;
}
return name; }

public static void main(String[] args) {

NimPlayer[] playerList = new NimPlayer[10]; // set an array here

Scanner in = new Scanner(System.in);
while (true) {
    System.out.print('$');
    String commandin = in.next();

    if (commandin.equals("addplayer")) { 
        String inName = in.nextLine();
        String[] name = splitName(inName);

        //Make sure the vadality of in name
        //Can use playerCheck to simplify the code
        if (name != null && name.length==3 && counter < 10) {
            if (playerList != null) {
                for (int i = 0; i < playerList.length; i++) {
                    String userCheck = playerList[i].getUserName();
                    if (userCheck.contains(name[0])) {
                        System.out.println("The player already exist.");
                    } else {
                        System.out.println("The player has been created.");
                        playerList[counter++] = new NimPlayer(name[0],name[1],name[2], 0, 0);
                    }
                }
            } else {
                // I have to add the player here
            }

        } else {
            System.out.println("Invalid input! e.g. addplayer george,Washington,George");
        }


    }

我的 NimPlayer 是:

public class NimPlayer {
private String userName;
private String familyName;
private String givenName;
private int score;
private int gamePlayed;



public NimPlayer(String userName, String familyName, String givenName, int gamePlayed, int score) {
    this.userName = userName;
    this.familyName = familyName;
    this.givenName = givenName;
    this.gamePlayed = gamePlayed;
    this.score = score;
}

public void setUserName(String userName) {
    this.userName = userName;
}
public void setFamilyName(String familyName) {
    this.familyName = familyName;
}
public void setGivenName(String givenName) {
    this.givenName = givenName;
}    
public String getUserName() {
    return userName;
}
public String getfamilyName() {
    return familyName;
}
public String getGivenName() {
    return givenName;
}
public int setScore(int score) {
    return score;
}
public int getScore() {
    return score;
}
public int setGamePlayed (int gamePlayed) {
    return gamePlayed;
}
public int getGamePlayed() {
    return gamePlayed;
}

}

【问题讨论】:

    标签: java oop constructor null


    【解决方案1】:
    1. 是的,您可以随意更改数组元素。所以“添加”玩家(在数组范围内设置数组元素的值)是可以的。
    2. 永远不会到达 else(“// 我必须在此处添加播放器”),因为 playerList 永远不会为空。

    由于playerList中的所有元素都被初始化为null,你应该在调用之前检查playerList[i]是否为null getUserName()(避免 NullpointerException)。

    【讨论】:

    • 感谢您的回复。如果我想经常检查playerList中的username是否相同;如果它为空,我仍然可以将播放器添加到列表中。我该怎么做这两件事?
    • 您可以在 for 循环之外定义一个布尔值 userExists,然后如果您在 for 循环中找到用户,则将其设置为 true。 for 循环之后,如果userExists,则将新用户放入playerList[counter++]。 (在您当前的代码中,如果在 playerList[0] 中找不到用户,您将添加用户,即使它可能在另一个索引上。)(在您使其工作后,您可以将现有用户的检查提取到 at 方法。注意hint "//可以使用playerCheck来简化代码"。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2021-12-27
    • 1970-01-01
    • 2018-12-02
    • 2020-05-29
    相关资源
    最近更新 更多