Can you explain if my answer is correct and why the others are not?
Which line should be added into the removeCard method of class GroupOfCards? public class GroupOfCards { private ArrayList<Card> cards; // list of cards // Remove and return a card object at a specified location public Card removeCard(int index) { ________________ } // end removeCard } // end class GroupOfCards
A. cards[index] = null;
B. Card = cards.get(index); return card;
C. cards.remove(index); (correct answer)
D. return cards.remove(index);
E. Can’t decide as we don’t know the detail of class Card
My Response:The ArrayList collection class contains the object cards and it provides the remove() method. Method remove(int index) is used for removing an element of the specified index from a list.
option a:
it places null value in the place of object, it does not remove the object from the list.
option b:
Card = cards.get(index); return card;
is used to read the value from the specified index and return the card object as specified in the function prototype..
option c:
cards.remove(index);
it is used to remove the value or object from the specified lndex.
but does not return any card Object. function need to return card object
option D: [correct answer]
return cards.remove(index);
we need to remove object or value from the specified location and need to return the object.
the above statement remove the object from index and return the card object.
Get Answers For Free
Most questions answered within 1 hours.