Results 1 to 5 of 5
- 08-28-2011, 04:41 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Removing an object from an arrayList while saving it at the same time.
Perhaps it's a newbie question... but I've seen question like "Remove a coin from the purse and then display it" but I never got such algorithm to work on mine.
Take for example my method:
Noticed that I have to resort to saving this "card" element into a reference data type "draw" before removing it and then return this "draw" to the main method calling it. I've tried the alternative that'sJava Code:public Card getCard(int g){ // Take int g from main() if (g>= 0&&g<cardList.size()){ // Check if there is any card in postion g [B][U][I] Card draw=cardList.get(g); // Save card in position g into card object draw cardList.remove(g); // Remove card in position g return [B]draw[/B];[/I][/U][/B] }else{ return null; }
but didn't worked.draw=cardList.remove(g;
return draw;
I wonder if theres a more efficient method in doing it, like to remove and capture the card at the same time and save to draw... or just do both of that and return the card without even using this "draw" thing.
In order to mimic real-world card game I also wanted to implement a function which would remove the "card"[object] from player's hand[arrayList] so if theres any shortcut method it would make the code a lot shorter.Last edited by FiasseKrystella; 08-28-2011 at 05:07 PM. Reason: Noticed something wrong with my code.
- 08-28-2011, 04:47 PM #2
Please explain what the program did. What does "didn't work" mean?but didn't worked.
- 08-28-2011, 05:04 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Actually Norm I just wanted to find a way to get the removal and adding/displaying done more efficiently.
The getCard method will take in an integer value from main() and check whether the cardList has this element, if it does then it will run this 3 lines :
if (g>= 0&&g<cardList.size()){
Card draw=cardList.get(g);
cardList.remove(g);
This is to get the specific card at int g position.
If not it would return a null value.
I've tried a few possiblities to shorten the code, making it more efficient. Such as:
draw=cardList.remove(g);
return draw;
But no result. (Couldn't compile and gives error).
- 08-28-2011, 05:09 PM #4
We can't help you unless you post the full text of the error messages.(Couldn't compile and gives error).
The first code does not need to use the get method as the remove method returns the element as you show in the second piece of code.
- 08-28-2011, 05:15 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Help removing items from an Arraylist and placing into a 2D Array
By david522 in forum New To JavaReplies: 8Last Post: 08-18-2011, 01:51 AM -
[HELP] Removing objects from an ArrayList!
By bpx95 in forum New To JavaReplies: 4Last Post: 04-30-2011, 05:54 AM -
removing repeated entries in arraylist
By ankit1801 in forum New To JavaReplies: 1Last Post: 04-15-2011, 06:34 AM -
help with saving objects in ArrayList
By anghel.michael in forum New To JavaReplies: 4Last Post: 02-22-2011, 09:09 PM -
Removing from an ArrayList while looping thru
By pahiker in forum New To JavaReplies: 7Last Post: 06-22-2010, 07:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks