[英]How to deal with null in an array when iterating
I am dealing with a classical Nim game project.我正在处理一个经典的 Nim 游戏项目。 The hurdle for me is to "only" use an array to save player in an object array, which means I will always get NullPointerException
when testing.我的障碍是“仅”使用数组将播放器保存在 object 数组中,这意味着我在测试时总是会得到NullPointerException
。 And I've searched almost all the information that there is no such a way to handle this problem.而且我搜索了几乎所有没有这样的方法来处理这个问题的信息。
One way to deal with it is to add if arr[i] != null
for checking non-null
object when iterating, but then, I need to write it for every method I created.处理它的一种方法是添加if arr[i] != null
以在迭代时检查non-null
object ,但是,我需要为我创建的每个方法编写它。 Is there any way to pass only the non-null
array to be checked?有没有办法只传递要检查的non-null
数组?
Here is part of my code Nimsys
:这是我的代码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");
}
}
And, below is part of my NimPlayer
class.而且,下面是我的NimPlayer
class 的一部分。
//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
}
You can do as below,您可以执行以下操作,
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");
}
Note that NimPlayer.getCounter()
is replaced with arr length to avoid ArrayIndexOutOfBoundsException
注意NimPlayer.getCounter()
被替换为 arr 长度以避免ArrayIndexOutOfBoundsException
Update: You can add the logic in getPlayer()
更新:您可以在getPlayer()
中添加逻辑
public static NimPlayer [] getPlayer() {
NimPlayer[] nimPlayers = Arrays.stream(playerList)
.filter(Objects::nonNull)
.toArray(NimPlayer[]::new);
counter = nimPlayers.length; //update the counter
return nimPlayers;
}
Try to create a non-null array outside the given section of the program.尝试在程序的给定部分之外创建一个非空数组。 In the sense, use a loop to create a non-null array that you can use throughout both programs and you can probably store that in a method or instance variable.从某种意义上说,使用循环创建一个非空数组,您可以在两个程序中使用它,并且您可以将其存储在方法或实例变量中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.