如何检查数组中的重复对象

How to check the duplicate object in the array

我是一个 java 初学者,我正在设计一个 Nim 游戏供许多玩家加入。我做了一些研究,但我不知道我的实施是否正确。目的是检查数组中的重复对象。我已经查阅了一些文章,我将在本文的最后部分引用它们。

对于 NimPlayer class。我创造了一些东西。

  1. 我已经定义了 NimPlayer 对象类型。
  2. 使用类型,我可以在有限的范围内初始化播放器space。
  3. 我按照这里的步骤初始化一个保存玩家数据的数组:

    public class NimPlayer {
    String userName;
    String familyName;
    String givenName;
    NimPlayer [] playerList = new NimPlayer[10]; //set an array here
    int id;
    
    //define NimPlayer data type
    public NimPlayer(String userName,String surName, String givenName) {
        this.userName = userName;
        this.familyName = surName;
        this.givenName = givenName;
    }
    //create new data using NimPlayer data type
    public void createPlayer(String userName, String familyName, String givenName) {   
        playerList[id++] = new NimPlayer(userName, familyName, givenName);
    }
    

在main方法中,我创建了一些功能供玩家使用:

  1. addplayer - 让用户可以在游戏中添加玩家进行比赛。
  2. 添加播放器,语法如下:

    $addplayer userName,familyName,givenName
    
  3. 为了验证输入,我拆分了输入并将它们存储在新对象中。

    public static String[] splitName(String inputName) {
    String [] splittedLine = inputName.split(",");
    String userName = splittedLine[0].trim();
    String familyName = splittedLine[1].trim();
    String givenName = splittedLine[2].trim();
    String [] name = new String[3];
    name[0] = userName;
    name[1] = familyName;
    name[2] = givenName;
    return name;
    }
    
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    
    //create new object to save data
    NimPlayer playerData = new NimPlayer(null, null, null);
    
    System.out.print('$');
    String commandInput = in.next();
    
    while (true) {
    
        if (commandInput.equals("addplayer")) {
            String inputName = in.nextLine();
            String[] name = splitName(inputName);
            String userName = name[0];
            String familyName = name [1];
            String givenName = name[2];
    
            playerData.createPlayer(userName, familyName, givenName);
    
            for (int i = 0; i < playerData.playerList.length; i++) {
                NimPlayer player = playerData.playerList[i];
                System.out.println(player.getUserName()); }
    }
    

    到目前为止,我有两个问题。

  4. 每次输入一组数据时,似乎我的"playerData"在循环对象时引发了NullPointerException,但由于我输入的名称是多个,所以我必须创建一个新的保存输入的主要方法中的对象。

  5. 为了检查 "inputName" 的集合中是否存在重复的 "userName",我循环遍历数组中的对象。在这种情况下如何访问 "userName"?

为了检查重复项,我检查了:

  1. What is a NullPointerException, and how do I fix it?
  2. Java Array, Finding Duplicates

您应该在 design/code 中解决以下问题:

  1. 由于您正在使用 createPlayer(String userName, String familyName, String givenName) 创建播放器,因此您应该将构造函数 NimPlayer(String userName,String surName, String givenName) 设为私有,这样就无法从 class、[=13] 外部调用它=].此外,将 createPlayer 声明为 static,这样它就不需要调用 NimPlayer 对象。
  2. 您需要有一个 static 计数器来跟踪玩家数量并在将新玩家添加到 playerList 之前检查此计数器的值。
  3. 您还应该在 inputName.split(",") 之后检查结果数组的大小。同样,在从 splitName 访问任何元素之前,您应该检查从 splitName 返回的数组的大小。

下面给出的是包含上述几点的代码:

import java.util.Scanner;

class NimPlayer {
    private String userName;
    private String familyName;
    private String givenName;
    //...
    // public getters and setters of userName, familyName, and givenName
    //...
    private static int counter = 0;
    private static NimPlayer[] playerList = new NimPlayer[10];

    private NimPlayer(String userName, String familyName, String givenName) {
        this.userName = userName;
        this.familyName = familyName;
        this.givenName = givenName;
    }

    public static void createPlayer(String userName, String familyName, String givenName) {
        if (counter < 10) {
            playerList[counter++] = new NimPlayer(userName, familyName, givenName);
        } else {
            System.out.println("The list is full.");
        }
    }

    public static int getCounter() {
        return counter;
    }

    public static NimPlayer[] getPlayers() {
        return playerList;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.print('$');
            String commandInput = in.next();
            if (commandInput.equals("addplayer")) {
                String inputName = in.nextLine();
                String[] name = splitName(inputName);
                if (name != null && name.length == 3) {
                    NimPlayer.createPlayer(name[0], name[1], name[2]);
                }
            } else {
                break;
            }
        }
        for (int i = 0; i < NimPlayer.getCounter(); i++) {
            System.out.println(NimPlayer.getPlayers()[i].getUserName());
        }
    }

    public static String[] splitName(String inputName) {
        String[] splittedLine = inputName.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;
    }
}

我不明白你的另一个问题:

For checking if there is the duplicate "userName" in the set of the "inputName", I loop through the objects in an array. How can I access the "userName" in this situation?