Results 1 to 9 of 9
Thread: Method help got stuck
- 03-29-2010, 11:46 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 46
- Rep Power
- 0
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
*/
Requirements: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(); }
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.
- 03-30-2010, 12:27 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
- 03-30-2010, 02:18 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 46
- Rep Power
- 0
I am not sure if i am lost but it doesn't make any sense to me
-
What gcalvin posted seems straightforward. What part about it confuses you?
- 03-30-2010, 02:55 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 46
- Rep Power
- 0
Is hi talking just of getting rid of the
or re doing the entire method?Java Code:ArrayList<File> output = new ArrayList<File>();
-
- 03-30-2010, 03:05 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 46
- Rep Power
- 0
Is there something else that i could do instead of re-writing the entire method?
- 03-30-2010, 03:13 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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-
-
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
-
I'm stuck help!!!
By nobody58 in forum Advanced JavaReplies: 2Last Post: 03-18-2010, 02:52 PM -
Stuck in sea
By programmer_007 in forum JDBCReplies: 1Last Post: 09-17-2009, 04:00 AM -
Im on my last lab!!!! And im stuck...:(
By clanboru15 in forum New To JavaReplies: 5Last Post: 03-13-2009, 01:44 AM -
really stuck now..
By shongo in forum Advanced JavaReplies: 2Last Post: 11-09-2008, 02:56 AM -
Stuck in need of help!
By Zombie_Leg! in forum New To JavaReplies: 1Last Post: 09-23-2008, 02:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks