I'm trying to create a program that will search through a File Array and store the unique file names into a new File Array and return that new Array, only putting duplicates in once (if there are any). My code runs through, but does not store the values into the new File Array I have created, which has no set length. It only returns an empty array when I call it. The way I have it setup, is it checks to see if there are any duplicates, if so it stores the duplicate once, if not it just stores the value and continues. The problem is that it won't store those values once it has been ran through the for loops. Is there a better way to store values in a File Array?
Here is my method uniqueFile which receives a File Array from my tester block.
public static File[] getUnique(File[] files) {
int count = 0, place = 0;
File[] newFile = new File[] {};
for (int i = 0; i < files.length; i++) {
count = 0;
for (int x = 1; x < files.length; x++) {
if (files[i].equals(files[x]))
count++;
}
try {
if (count >= 1)
newFile[place] = files[i];
else
newFile[place] = files[i];
} catch (Exception e) {
}
place++;
}
return newFile;
}
This is my tester block:
{
File Freckle = new File("Freckle");
File Pickle = new File("Pickle");
File Sam = new File("Sam");
File Cat = new File("Cat");
File[] files = new File[] { Freckle, Pickle, Freckle, Sam, Cat,
Pickle };
File[] output = ArrayExercises.getUnique(files);
System.out.println(Arrays.toString(output));
}
I put in generic File names to test to see if it would work. Eventually I will incorporate actual files, but I want to get this bug figured out first before I continue.
freckle, notFreckle. This helps other readers differentiate variables names from classes, which are capitalised.