gpt4 book ai didi

java - 迭代时如何处理数组中的 null

转载 作者:行者123 更新时间:2023-12-02 08:40:17 25 4
gpt4 key购买 nike

我正在处理一个经典的 Nim 游戏项目。对我来说,障碍是“仅”使用数组将玩家保存在对象数组中,这意味着在测试时我总是会遇到 NullPointerException 。而且我查遍了几乎所有的资料都没有这样的方法来处理这个问题。

处理它的一种方法是添加 if arr[i] != null 用于在迭代时检查 non-null 对象,但是随后,我需要编写它适用于我创建的每个方法。有没有办法只传递要检查的非空数组?

这是我的代码的一部分Nimsys:

public static void main(String[] args) {
System.out.println("Welcome to Nim\n");
Scanner in = new Scanner(System.in);
while (true) {
System.out.print('$');
String commandin = in.next();

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

if (commandin.equals("removeplayer")) {
String user = in.nextLine().trim();

if (user.equals("")) {
System.out.println("Are you sure you want to remove all players? (y/n) \n");

commandin = in.next();
if (commandin.equals("y")) {
for (int i = 0; i < NimPlayer.getCounter(); i++) {
NimPlayer.getPlayer()[i] = null;
}
System.out.println("Remove all the players");
}
}
if (!user.equals("")) {
searchAndRemovePlayer(user);
}
}
public static void searchAndRemovePlayer(String user) {
for (int i = 0; i < NimPlayer.getCounter(); i++) {
String userName = NimPlayer.getPlayer()[i].getUserName().trim();
if (userName.equals(user)) {
NimPlayer.getPlayer()[i] = null;
System.out.println("Remove successfully!\n");// A test to see if the code runs
return;
}
}
System.out.println("The player does not exist.\n");
}
}

下面是我的 NimPlayer 类的一部分。

//username, given name, family name, number of game played, number of games won
public class NimPlayer {
private String userName;
private String familyName;
private String givenName;
private int score;
private int gamePlayed;

private static int counter;
private static final int SIZE = 5;
static NimPlayer[] playerList = new NimPlayer[SIZE]; // set an array here

//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 static void createPlayer(String userName, String familyName, String givenName) {
if (counter<SIZE) {
playerList[counter++] = new NimPlayer(userName, familyName, givenName);
} else {
System.out.println("Cannot add more players.");
}
}
public static int getCounter() {
return counter;
}
public static NimPlayer [] getPlayer() {
return playerList;
}

//getters and setters

}

最佳答案

您可以执行以下操作,

public static void searchAndRemovePlayer(String user) {
NimPlayer[] players = Arrays.stream(NimPlayer.getPlayer())
.filter(Objects::nonNull)
.toArray(NimPlayer[]::new);
for (int i = 0; i < players.length; i++) { // replaced NimPlayer.getCounter() to avoid Index Out of Bound
String userName = players[i].getUserName().trim();
if (userName.equals(user)) {
players[i] = null;
System.out.println("Remove successfully!\n");// A test to see if the code runs
return;
}
}
System.out.println("The player does not exist.\n");
}

请注意,NimPlayer.getCounter() 已替换为 arr 长度,以避免 ArrayIndexOutOfBoundsException

更新:您可以在 getPlayer()

中添加逻辑
 public static NimPlayer [] getPlayer() {
NimPlayer[] nimPlayers = Arrays.stream(playerList)
.filter(Objects::nonNull)
.toArray(NimPlayer[]::new);
counter = nimPlayers.length; //update the counter
return nimPlayers;
}

关于java - 迭代时如何处理数组中的 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61423932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com