简体   繁体   中英

Array of objects - Removing an object from the array

I have been assigned the task of creating the infamous "UNO" game. I am running into trouble with one of the methods, which is supposed to "delete" one of the card objects in the players hand if they choose to play it. I suppose you have to have played UNO to understand. Anyways, here is the description of what the method should be:

Create a method named removeCardFromHand that takes an integer index as a parameter and both removes and returns the Card at that position of the player's hand. If the specified index does not correspond to one of the cards in the hand, the method should throw an IndexOutOfBoundsException. After the card has been removed, the remaining cards should be rearranged as needed so that they occupy the leftmost positions of the array. One way to do this is to move the rightmost card into the position of the card that is being removed. For example, if the hand is currently the four cards {blue 3, red 2, yellow 7, green 1} and you are removing the card at position 1 (the red 2), you could replace it with the last card (the green 1), and thus the resulting hand would be: {blue 3, green 1, yellow 7}.

I am unfortunately not allowed to use an Array List or Vectors, just simple old arrays

Here is my code so far:

public Card removeCardFromHand(int n)
{
    Card c = cards[n];
    for(int i = n; i < cards.length; i--)
    {
        cards[n] = cards[n + 1];
    }
    c = cards[cards.length - 1];
    return c;
}  

Obviously this isn't right, but I'm just not sure what to do.

You pretty much got it, except that you are moving all cards in the array one position to the left, so you are dropping the leftmost card, and you leave the "removed" card in the array, unless it happens to be the left most one. You want to be doing the moving only starting at the card to the right (higher index) of the removed card. Since I think this is homework, I'll let you figure out the one expression you need to change ;-)

Assuming the array is created with a static size. You would not be able to change the array size by reassigning the length of the array. Probably giving you errors about trying to do that.

In the Class

Add a new variable in the Class, ie. int numberOfCardsInHand.

In the method removeCardFromHand

Make a copy of the card to discard, i.e. discardCard.

Iterate from i = n to i < numberOfCardsInHand
  Left shift the remaining cards to the right of the discarded card.

Decrement numberOfCardsInHand by 1.

Return discardCard

You are moving all the cards to the left. Consider where the nth card is. You only need to move those to the right of that card. Also, the first iteration of your loop will try to do this

cards[0] = cards[0 - 1];

That's probably not what you really want to have happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM