Reputation: 23
I want to move a specific value from an array list to another, what is the best way to go about doing this? Here is some example code:
values from
ArrayList<CreateCard> deckArray = new ArrayList<CreateCard>();
to
ArrayList<CreateCard> playerHand = new ArrayList<CreateCard>();
Upvotes: 1
Views: 1557
Reputation: 3
I would do a function like :
public void moveCard(List<CreateCard> playerHand, List<CreateCard> deckArray) {
playerHand.add(deckArray.get[0])
deckArray.remove(0)
}
Hope it helps.
Upvotes: 0
Reputation: 7334
I'm a big fan of helper functions:
public static void main(String[] args) {
ArrayList<CreateCard> deckArray = new ArrayList<CreateCard>();
deckArray.add(new CreateCard());
deckArray.add(new CreateCard());
ArrayList<CreateCard> playerHand = new ArrayList<CreateCard>();
// Deal a card
moveASpecificValue(deckArray, playerHand, 0);
// Deal from the bottom of the deck
moveASpecificValue(deckArray, playerHand, deckArray.size() - 1);
}
public static void moveASpecificValue( List<CreateCard> source,
List<CreateCard> destination,
int indexOfSpecificValue) {
destination.add(source.remove(indexOfSpecificValue));
}
Upvotes: 1
Reputation: 1280
Some things first.
CreateCard
is a class representing what? If it just represents a card then I'd name the class Card
to avoid confusion.DeckArray
to deckArray
.deckOfCards
instead of deckArray
as deckArray
could be interpenetrated as an array of decks.ArrayList
.Last the actual problem.
PlayerHand.add(DeckArray.remove(0));
That would remove the top card and add it to the PlayerHand.
Upvotes: 1
Reputation: 1
Just for loop through the ArrayList that you want to get the value from and when you come to the right index or criteria then just place the value inside the other array list.
Upvotes: 0
Reputation: 21
If you have the index i where the CreateCard is stored in DeckArray, you can just do:
PlayerHand.add(DeckArray.remove(i));
Upvotes: 1