Results 1 to 9 of 9
  1. #1
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default Method help got stuck

    This is what i got so far but the problem is i did not realize that we could not use the ArrayList to search through and now i don't know how to change it so i don't use it that is the only way i know. Please help
    /**
    * This method creates and returns a new array that contains a list of the
    * unique File objects that occur in the files array. For this method,
    * File objects are the same if they are equal using the .equals method.
    * <p>
    *
    * The size of the returned list is determined by the number of unique
    * items in the original list. The order of the items in the returned
    * list is unspecified.
    * <p>
    *
    * The files array must not contain null.
    * <p>
    *
    * <p><b>You will write this method as part of programming assignment #9.</b>
    *
    * @param files An array of File objects, possibly containing duplicates
    * @return An array of unique File objects
    * @throws NullPointerException If files is null or files contains null
    */
    Java Code:
        public static File[] getUnique (File[] files)
        {
        	 ArrayList<File> output = new ArrayList<File>();
    
             for(int i = 0; i < files.length; i++)
             {
                  boolean copy = false;
    
                  for(int j = 0; j < output.size(); j++)
                  {
                       if(files[i].equals(output.get(j)))
                       {
                            copy = true;
                            break;
                       }
                  }
    
                  if(!copy)
                  {
                       output.add(files[i]);
                  }
             }
    
             return (File[]) output.toArray();  
        }
    Requirements:
    For getUnique, the input array {new File ("Foo"), new File ("Bar"), new File ("Foo")} should return an array containing two file objects - File objects representing files "Foo" and "Bar".
    Do not use library classes to manipulate or search through arrays. (The Arrays class is not allowed.)
    This method should create and return a new array of File objects. The array should contain the unique File objects found in the input array. The input array should not be changed.

    Note that you do not need to create new File objects, just a new array of File objects.

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Make an array of booleans the same size as your files array -- call it duplicates. Loop once through the files, comparing each file to all the files before it in the array, and marking the duplicates in your boolean array. So files[0] can't be a duplicate, which means duplicates[0] = false. You check files[1] and if it's the same as files[0] then duplicates[1] gets set to true, otherwise it gets set to false. Continue, comparing files[2] to files[0] and files[1] and so on. Keep a running count each time you set a false in duplicates -- that will be the size of your result array. Then loop again and copy files[i] if duplicates[i] is false.

    You could do it in one pass, except for the requirement that your returned array has to be sized properly.

    -Gary-

  3. #3
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    I am not sure if i am lost but it doesn't make any sense to me

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Is hi talking just of getting rid of the
    Java Code:
     ArrayList<File> output = new ArrayList<File>();
    or re doing the entire method?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by bdario1 View Post
    Is hi talking just of getting rid of the
    Java Code:
     ArrayList<File> output = new ArrayList<File>();
    or re doing the entire method?
    He's talking about doing a complete re-write using his steps as an outline.

  7. #7
    bdario1 is offline Member
    Join Date
    Mar 2010
    Posts
    46
    Rep Power
    0

    Default

    Is there something else that i could do instead of re-writing the entire method?

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    There are lots of other things you could do, but I don't see how you can satisfy the requirements of your assignment without taking two passes through the array. You can't create your result array until you know what size it needs to be, and you won't know that until you know how many duplicates you have, and you won't know that until you check for them. That says to me that you can't fill your result array while you're checking for duplicates. But maybe you can think of a way. Good luck!

    -Gary-

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I suppose you could create a temp array the same size as the original array, copy values to the temp array if not duplicated, keeping count of when you copy over, then create a return array of count size, and then use System.arraycopy to recopy from the temp array to the return array.

Similar Threads

  1. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 03:52 PM
  2. Stuck in sea
    By programmer_007 in forum JDBC
    Replies: 1
    Last Post: 09-17-2009, 05:00 AM
  3. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 02:44 AM
  4. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 03:56 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 03:22 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
  •