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

问题描述 投票:1回答:1

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

对于NimPlayer类。我已经创建了一些东西。

  1. 我已经定义了NimPlayer对象类型。
  2. 使用类型,我可以在有限的空间中初始化播放器。
  3. 我按照此处的步骤初始化一个用于保存玩家数据的数组:Storing object into an array - Java

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

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

  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,但是由于我的名字输入是多个,因此我必须在main方法中创建一个新对象来保存输入。

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

为了检查重复项,我已经检查过:

  1. Java - Loop through instances of a class rather than calling a method for each separate instance
  2. What is a NullPointerException, and how do I fix it?
  3. Java Array, Finding Duplicates
java duplicates java.util.scanner
1个回答
0
投票

您应该解决然后在您的设计/代码中进行以下操作:

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

下面给出的代码结合了上述要点:

import java.util.Scanner;

class NimPlayer {
    private String userName;
    private String familyName;
    private String givenName;
    // 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;
    }
}

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

用于检查是否存在重复的“用户名”“ inputName”,我遍历数组中的对象。我该如何进入在这种情况下的“用户名”?

© www.soinside.com 2019 - 2024. All rights reserved.