0

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.

2
  • Why not just use a Set<File> instead? Commented Feb 20, 2013 at 6:39
  • It's good practice in Java to have your variable names in lower case, eg freckle, not Freckle. This helps other readers differentiate variables names from classes, which are capitalised. Commented Feb 20, 2013 at 6:42

2 Answers 2

3

You are making things quite difficult for yourself. Let Java do all the work for you.Try using LinkedHashSet, as it gives you uniqueness and also, retains insertion order. It will also be more efficient than comparing every value with every other value.

File [] input = {Freckle, Pickle, Freckle, Sam, Cat, Pickle};
Set<File> tmp = new LinkedHashSet<File>();
for (File each : input) {
    tmp.add(each);
}
File [] unique = new File[tmp.size()];
int i = 0;
for (File each : tmp) {
    unique[i++] = each;
}
System.out.println(Arrays.toString(unique));
Sign up to request clarification or add additional context in comments.

Comments

0

As others have said, you should use the Java Collections API, it makes life so much easier. But let's say for a moment you want to get your solution to work.

he problem is your new array is zero length, and here you have a very strange piece of code.

     if (count >= 1)
            newFile[place] = files[i];
        else
            newFile[place] = files[i];

the test is meaningless, you're doing exactly the same thing irrespective of the value of count. You also need to only increment place when you add non-duplicate string to your array. Also the try/catch is pointless. it' bad practice to catch a generic exception.

What you nee is more like below, but even this won;t do exactly want you want s although the array will now only contain unique entries, it ill be the same length as before.

public static File[] getUnique(File[] files) {
    place = 0;
    File[] newFile = new File[files.size()]; //you were creating an empty array.
    for (int i = 0; i < files.length; i++) {
        boolean duplicate = false; // not interested in ho many dupes, just if there is one. 
        for (int x = 1; x < files.length; x++) {
            if (files[i].equals(files[x])) {
                duplicate = true;
                break; // no point in checking the rest.
             }
        }
        //   why on earth did you have a try catch?
        if (!duplicate) {
            newFile[place++] = files[i];
         }

    }

    return newFile;
}

What you need to do really is to throw this away and start again using something like the LinkedHashMap as another poster suggests, otherwise you're tying yourself in knots with inefficient code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.