我正在处理一个经典的尼姆游戏项目。对我来说,障碍是"只"使用数组来保存对象数组中的播放器,这意味着我在测试时总是会得到NullPointerException
。我搜索了几乎所有的信息,发现没有这样的方法来处理这个问题。
处理它的一种方法是添加if arr[i] != null
,用于在迭代时检查non-null
对象,但之后,我需要为我创建的每个方法编写它。有没有办法只通过要检查的non-null
数组?
这是我的代码Nimsys
:的一部分
public static void main(String[] args) {
System.out.println("Welcome to Nimn");
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;
}
尝试在程序的给定部分之外创建一个非null数组。从这个意义上说,使用循环创建一个非null数组,可以在两个程序中使用,并且可以将其存储在方法或实例变量中。