Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2009
    Rep Power

    Angry Compare two arrays for difference

    Hi all, this has got me stumped. I have two arrays of the same type of objects. I want to compare each of the values in the first array to see if they exist in the second array. If a value does not exist in the second array I want to store that value in a third array for use later in my program.

    Obj[] array1 = {1,2,3,4,5,6,7,8};
    Obj[] array2 = {1,3,5,6,7},

    So after comparing array1 to array2, I want array3 to have the values:

    Obj[] array3 = {2,4,8}


  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Cambridge, UK
    Rep Power


    Perhaps you want to use Set and its methods? This operation would be a set difference.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Rep Power

    Default Using the Set class.... interesting

    What OD suggests is interesting. A Set is a collection that contains no duplicate elements.
    Set (Java Platform SE 6)
    Creating a Set (Java Developers Almanac Example)
    What do can do is check each element in one set against the set using the contains() method.

    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. How to Compare two Dates
    By Java Tip in forum java.util
    Replies: 1
    Last Post: 06-24-2008, 07:05 AM
  2. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 07:04 PM
  3. Compare lists
    By JavaNoob in forum New To Java
    Replies: 2
    Last Post: 08-08-2007, 03:11 PM
  4. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 03:57 AM
  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