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
*/
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.equals(output.get(j)))
{
copy = true;
break;
}
}
if(!copy)
{
output.add(files);
}
}
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.
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
*/
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.equals(output.get(j)))
{
copy = true;
break;
}
}
if(!copy)
{
output.add(files);
}
}
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.