我正在尝试将数组的值更改为空。
我有一个名为 card[100] 的数组。 在这个数组中,我存储了 100 张卡。每张卡包含两个 int 值和一个字符串值:
public Card(int manaCost, int damageDone, java.lang.String name) {
每个数组索引都已填充,但我希望能够在调用另一个方法时将值设置为 null。我尝试了以下方法:
array[index] = null;
- 这会导致错误array[index] = new array(null, null, null);
- 一个
rray tempArray = null; array[index] = tempArray;
当我觉得这应该很容易解决时,我对如何将数组项设置为 null 感到非常困惑。
这是这个类的完整代码,我正在努力的方法就是getRandomCard:
public class Deck {
private Card[] card = new Card[100];
private int cardsRunning = 100;
static int DECKSIZE = 100;
static java.lang.String emailID = "MITNY013";
private boolean random = false;
/**
* Constructor for the Deck. Adds the following cards to the deck in the following order
* 4x Super Lucky Strike, Damage 100, Mana 2
* 6x Mega Santa Hit, Damage 80, Mana 2
* 10x Critical Hit, Damage 50, Mana 5
* 10x Massive Strike, Damage 40, Mana 7
* 15x Wrong Way Down A One Way Street, Damage 30, Mana 10
* 15x Bender Rules Here, Damage 15, Mana 10
* 40x Trade, Damage 5, Mana 5 <br>
* My solution length: 32 lines (space lines not included)
* @param random - Whether to turn on random features
*/
public Deck(boolean random) {
if (random == false) {
for (int index = 0; index <= 3; index++) {
card[index] = new Card( 2, 100, "Lucky Strike");}
for (int index = 4; index <= 10; index++) {
card[index] = new Card( 2, 80, "Santa Hit");}
for (int index = 11; index <= 20; index++) {
card[index] = new Card( 5, 50, "Critical Hit");}
for (int index = 21; index <= 30; index++) {
card[index] = new Card ( 7, 40, "Massive Strike");}
for (int index = 31; index <= 45; index++) {
card[index] = new Card( 10, 30, "Wrong Way Down A One Way Street");}
for (int index = 46; index <= 60; index++) {
card[index] = new Card( 10, 15, "Bender Rules Here");}
for (int index = 61; index <= cardsRunning-1; index++) {
card[index] = new Card( 5, 5, "Trade");} }
else if (random == true) {
for (int index = 0; index <= 3; index++) {
card[index] = new Card( 2, 100, "Lucky Strike");}
for (int index = 4; index <= 10; index++) {
card[index] = new Card( 2, 80, "Santa Hit");}
for (int index = 11; index <= 20; index++) {
card[index] = new Card( 5, 50, "Critical Hit");}
for (int index = 21; index <= 30; index++) {
card[index] = new Card ( 7, 40, "Massive Strike");}
for (int index = 31; index <= 45; index++) {
card[index] = new Card( 10, 30, "Wrong Way Down A One Way Street");}
for (int index = 46; index <= 60; index++) {
card[index] = new Card( 10, 15, "Bender Rules Here");}
for (int index = 61; index <= cardsRunning-1; index++) {
card[index] = new Card( 5, 5, "Trade");}
shuffle(card);}
}
/**
* Returns a string representation of the entire deck in the format
* Deck [ 1:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
* 2:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
* 3:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]
* 4:Card [name=Super Lucky Strike, manaCost=2, damageDone=100]] <br>
* My solution length: 6 lines
* @overrides - toString in class java.lang.Object
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int index = 0; index < card.length; index++) {
sb.append((index + 1) + ": " + card[index].toString() + "n");}
return sb.toString();
}
/**
* Uses a random number generator to get a card from the deck somewhere then swaps the
* last card in the deck to that position and reduces the cardsRemaining by one.
* This is important. if the random flag is false you should always get the card at
* position 0. When you swap the card out you should also set the old position to null
* for safety. <br>
* My solution length: 6 lines
* @return - the card drawn or null if no cards left in deck
*/
public Card getRandomCard() {
if (cardsRunning == 0) {
return null;
}
// use random number generator to generate a number to get a card from the deck
Random rand = new Random();
int randomCardNo = rand.nextInt(cardsRunning);
// created newCard in order to return the original random card as it will be replaced by the last value
//in the partially fill array
Card newCard = card[randomCardNo];
//reorder the array - swaps the last card in the deck to the position of the card being removed
card[randomCardNo] = card[cardsRunning-1];
//Set the card[cardsRunning -1] to null value so it is removed form the deck
card[cardsRunning-1] = null;
cardsRunning--;
return newCard;
}
/**
* Return the number of cards remaining in the deck.
* My solution length: 1 lines
* @return - the total cards remaining
*/
public int getCardsRemaining() {
return this.cardsRunning;}
/**
* BONUS Method
* My solution length: 6 lines
*/
public void shuffle(Card[] card) {
Random rand = new Random();
for (int index = 0; index < card.length; index++) {
int randomIndexToSwap = rand.nextInt(card.length);
Card temp = card[randomIndexToSwap];
card[randomIndexToSwap] = card[index];
card[index] = temp;}
}
如前所述,该错误是 NullPointerException,但我不确定如何将 card[] 转换为列表,因为它有 3 个变量。嗯嗯
感谢您的帮助,
类似的东西
card[index]=null;
本身不会抛出错误。但是,在此之后,任何在该索引处操作元素的尝试都将开始抛出NullPointerException
,这是合乎逻辑的。
因此,处理此问题的一种方法是引入空卡或丢失卡的概念。也许是通过向 Card 类添加一个布尔字段,然后在使用该卡执行任何操作之前检查该card[index].isEmpty()==false
。
但是,如果您将数组更改为ArrayList,则可能会更合适和直观,以便根据需要轻松完全删除或添加元素,就像HimBromBeere建议的那样