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 5 of 5

Thread: Is this a stupid idea?

  1. #1
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Is this a stupid idea?

    Hi all.

    Had to refactor the program I am working on after deciding that a feature was missing. Anyway, I realized that a private method was useful elsewhere and moved it out. Then I refactored that method as well, so it . Anyway, long story short I ended up with this method:
        public static void fill(Collection<String> data, Iterator<String> itr) {
            while (itr.hasNext())
                data.add(itr.next());
        }
    Is that too little code to justify being in a method of its own? I feel that it helps keeping my code a but cleaner to have it as a utility method rather then doing it over and over again every time I need to do that (it is not too many times, tbh, but its common enough). This is also related to another question, which is the one that I was thinking of when I made the title, but I thought I would ask this one first since they kind of goes hand in hand.

    Take care,
    Kerr.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Is this a stupid idea?

    If your aim is to add items from one Collection into another then why not use the addAll method?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Is this a stupid idea?

    I have to use an Iterator and the addAll method accepts a Collection.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Is this a stupid idea?

    Why do you have to use an Iterator? If this is for a school assignment then make sure you have read the requirements correctly. Confirm with the teacher if you can use the addAll method.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Mar 2011
    Location
    Earth!
    Posts
    77
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Is this a stupid idea?

    No, the program is just me fooling around building a very basic scripting language (quite useless, actually, since it can only do about 4 things, but its good training). Basically I am using an iterator to move through a string that is being read from a file. There is a class that does that already, moving through a string the way I want that is, but I didnt realize that until after I made the iterator, and since my code works as I want I didnt feel the need to change it. Anyway, the iterator is used for some things before this method is called. In fact, this method is the one called last, when the rest of the string is data rather then commands.

    In any case, I have decided that my change was not needed so I changed back my code. Would still like an answer to the question, though, just for future reference.
    Last edited by Kerr; November 30th, 2011 at 01:34 PM.

Similar Threads

  1. what the stupid problem in using netbeans
    By ms_ceng in forum Java IDEs
    Replies: 3
    Last Post: December 13th, 2011, 03:28 AM
  2. "The import ___ cannot be resolved" (stupid noob question)
    By RobG in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2010, 03:09 PM
  3. No idea what to do for this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2010, 07:16 PM
  4. Stupid thing can't find image icon right in front of it!!!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 9th, 2010, 01:02 AM
  5. My doubt may be stupid
    By kalees in forum Object Oriented Programming
    Replies: 2
    Last Post: September 22nd, 2009, 02:38 AM