Results 1 to 5 of 5
  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Compare two Arrays, checking for duplication

    Good day. First post here, so please bare with me.

    I have two directories containing files. I want to output the files from directory a that are also contained in directory b.

    C:/DIFFCHECK/DIR1/: TEST_DUP.txt, TEST_UNI.txt
    C:/DIFFCHECK/DIR2/: TEST_DUP.txt, TEST2.txt

    Desired output: TEST_DUP.txt is a duplicate.

    I believe I need to use a for loop vice nested if statements, but can't get my head around this one. Please help!

    What I have so far:

    import java.io.File;

    public class diffcheck2
    {
    public static void main(String[] args)
    {
    //populate the arrays
    File f = new File("C:/DIFFCHECK/DIR1/");
    File[] rec = f.listFiles();

    File g = new File("C:/DIFFCHECK/DIR2/");
    File[] hist = g.listFiles();

    int i = 0;
    int j = 0;
    if (rec[i].isFile() && hist[j].isFile())

    if (rec[i].getName().equals(hist[j].getName())) {

    System.out.println(rec[i].getName());

    }
    }

    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Compare two Arrays, checking for duplication

    Don't use two arrays of file names; use two Sets instead (read the API documentation for the Set interface and its implementations).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    farhanz2009 is offline Member
    Join Date
    Feb 2012
    Posts
    14
    Rep Power
    0

    Default Re: Compare two Arrays, checking for duplication

    The code below shows how i would have done it. Hope it helps :)

    [ no spoonfeeding please -- mod ]
    Last edited by JosAH; 02-03-2012 at 09:27 PM.

  4. #4
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Compare two Arrays, checking for duplication

    Hello JosAH. Please un-edit farhanz2009's post. I'd like to compare to code I have since written that I believe sucessfully solves the problem.
    Even with my lack of experience, I have found it is always benificial to view a problem from a different perspective. Thank you in advance :)

    My code (works):

    import java.io.File;

    public class diffcheck2
    {
    public static void main(String[] args)
    {
    //populate the arrays
    File f = new File("C:/DIFFCHECK/DIR1/");
    File[] rec = f.listFiles();

    File g = new File("C:/DIFFCHECK/DIR2/");
    File[] hist = g.listFiles();

    for(int i=0;i<rec.length;i++)
    { boolean found = false;
    for(int j=0;j<hist.length;j++){
    if((rec[i].getName().equals(hist[j].getName()))){
    found = true;
    break; } }
    if (found)
    {
    System.out.println("*Duplicate File: "+ rec[i].getName());
    }

    }
    }

    }
    Last edited by Redefine12; 02-03-2012 at 10:20 PM. Reason: Added politeness

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Compare two Arrays, checking for duplication

    We don't do any spoonfeeding in this forum; if you have tested your code and it works, you have found a solution similar to the other (spoonfeeding) reply. There's a much better way to do it though, as I suggested in my first reply: use Sets.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 11
    Last Post: 11-02-2011, 07:38 PM
  2. compare arrays
    By innspiron in forum New To Java
    Replies: 4
    Last Post: 04-16-2010, 10:22 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. Compare two arrays for difference
    By aaronfsimons in forum New To Java
    Replies: 2
    Last Post: 05-11-2009, 03:49 PM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 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
  •