Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default 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:
    Java 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;
        	 }
    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's
    draw=cardList.remove(g;
    return draw;
    but didn't worked.

    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    but didn't worked.
    Please explain what the program did. What does "didn't work" mean?

  3. #3
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default

    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).

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,463
    Rep Power
    25

    Default

    (Couldn't compile and gives error).
    We can't help you unless you post the full text of the error messages.

    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.

  5. #5
    Join Date
    Aug 2011
    Posts
    16
    Rep Power
    0

    Default

    Oh actually it's working now. Didn't knew why it gave me error the previous time.

    Thanks for helping me out, and sorry for the confusion.

Similar Threads

  1. Replies: 8
    Last Post: 08-18-2011, 01:51 AM
  2. [HELP] Removing objects from an ArrayList!
    By bpx95 in forum New To Java
    Replies: 4
    Last Post: 04-30-2011, 05:54 AM
  3. removing repeated entries in arraylist
    By ankit1801 in forum New To Java
    Replies: 1
    Last Post: 04-15-2011, 06:34 AM
  4. help with saving objects in ArrayList
    By anghel.michael in forum New To Java
    Replies: 4
    Last Post: 02-22-2011, 09:09 PM
  5. Removing from an ArrayList while looping thru
    By pahiker in forum New To Java
    Replies: 7
    Last Post: 06-22-2010, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •