Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Modifying the method to clear collection HELPPP!

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Modifying the method to clear collection HELPPP!

    I have wrote the method delete turtle below. Which is part of turtle collection (Hashmap)
    public void deleteTurtle(String name) {                                  
        Iterator iterate = turtles.keySet().iterator();
        while(iterate.hasNext()) {
            String key = (String)iterate.next();
            if(key.equals(name)) {
                iterate.remove();
            } else {
                setErrorMessage("notFound");
            }
        }
    }

    I need to now modify it so the method ensures that the turtles shape collection is cleared before the Turtle object is deleted.

    I am beginner and really confused any sort of help will be appreciated thanks.
    Last edited by miss-naina; May 11th, 2014 at 11:34 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,351
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Modifying the method to clear collection HELPPP!

    turtles shape collection is cleared before the Turtle object is deleted.
    Can you post the code you are having problems with? What are the variable definitions for the collection and object?
    If a Collection is cleared, there won't be anything to delete.


    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Modifying the method to clear collection HELPPP!

    Do you mean the entire code? before the delete method?
    Last edited by miss-naina; May 11th, 2014 at 11:34 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,351
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Modifying the method to clear collection HELPPP!

    so the collection is cleared .
    The Collection interface has a clear() method. Can that do what you want?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    miss-naina (May 11th, 2014)

  6. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Modifying the method to clear collection HELPPP!

    Yes, How can I use that Clear method? Please guide me
    There also a empty method that can be used do u know how that can be used?

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,351
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Modifying the method to clear collection HELPPP!

    Write a small test program that uses a Collection. Add some objects to the Collection, print it to see what is in it, then call the clear() method and print it again to see if it is empty.

    Also try using the empty method in the test program to see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    miss-naina (May 11th, 2014)

Similar Threads

  1. [SOLVED] error while creating a "clear" method
    By weirddan in forum Java Theory & Questions
    Replies: 8
    Last Post: January 16th, 2014, 03:11 PM
  2. Replies: 1
    Last Post: September 8th, 2012, 05:32 AM
  3. NULLeXCEPTION PROBLEM plz helppp
    By gamernayan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 23rd, 2012, 09:58 AM
  4. MergeSort not working Helppp please :(
    By colombianita2089 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 17th, 2010, 09:56 PM

Tags for this Thread