0

I'm trying to remove and return the object at the first index of my array list called deck. My code returns the object at index 0 post remove() being called, so it's always returning the second index of the original deck array list. I'm not sure how to return the index at 0, THEN remove it.

public Card dealCard()
{
    int i = 0;                
    Card topCard = null;
    if(deck.size() > 0)
    {
        topCard = deck.get(i);
        deck.remove(topCard);        
    }           
    else
    {
        System.out.println("Fatal Error. Program now exiting.");
        System.exit(0);
    }
    return topCard;
}     
6
  • Does it return the 0th obj when you comment "deck.remove(topCard); " ? Commented Apr 14, 2018 at 10:39
  • It looks correct to me. What makes you think it's wrong? Commented Apr 14, 2018 at 10:41
  • Yeah, it returns the 0th object when remove() isn't in play Commented Apr 14, 2018 at 10:43
  • It's wrong because it is returning the element at index 0 AFTER remove() is called, instead of returning the index at 0 of the original array list, THEN removing it. Commented Apr 14, 2018 at 10:46
  • No. topCard = deck.get(i) sets topCard to the card that was the i'th prior to being removed, then deck.remove(topCard) removes that card from deck but doesn't change topCard. Commented Apr 14, 2018 at 11:22

4 Answers 4

1

remove(int index) returns the element at index when you pass an index to it. So all you have to do is

if(deck.size() > 0)
{
    topCard = deck.remove(i);
}           

This will set topCard to whatever is at index i and remove the element from the list

Sign up to request clarification or add additional context in comments.

1 Comment

This still returns the first index after remove() is called, so it's missing the index 0 of the original deck array list.
0

Try this:

public Card dealCard()
{
    int i = 0;                
    Card topCard = null;
    //topCard.equals(deck.get(i));
    //deck.remove(topCard);
    if(deck.size() > 0)
    {
        topCard = deck.get(i);
        deck.remove(i);
        //temp = deck.remove(topCard);
        //temp.equals(deck.remove(topCard));           
    }           
    else
    {
        System.out.println("Fatal Error. Program now exiting.");
        System.exit(0);
    }
    return topCard;
}   

2 Comments

Still causing same issue unfortunately.
I'm not sure how to do that. Sorry, new here.
0

Don't remove what you store in the variable top card, instead remove the item by its index.

public Card dealCard() {
int i = 0;                
Card topCard = null;
if(deck.size() > 0)
{
    topCard = deck.get(i);
    deck.remove(i);        
}           
else
{
    System.out.println("Fatal Error. Program now exiting.");
    System.exit(0);
}
return topCard;

1 Comment

Still causes same issue.
0

Remove Item from list using index

deck.remove(i);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.