#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    26
    Rep Power
    0

    intersection of arrays


    Hi,

    How do I obtain the intersection of 2 arrays in Java ??

    I have a String array a[] and an ArrayList ai()... Need to obtain the values in a[] which are not present in ai()..

    hv browsed and browsed, tried creating my own scripts but to no avail..

    afaik there r no builtin function in jdk1.3 which does this..

    any suggestions???

    tia,
    gaucho
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    9
    Rep Power
    0

    Smile Intersection of two arrays.


    Hi.

    I think this will solve the problem.

    public class test33
    {
    public static void main( String args[] )
    {
    int[] array1 = { 2, 4, 6, 8, 10 };
    int[] array2 = { 2, 4, 5, 7, 9 };

    for( int i = 0; i < array1.length; i++ )
    {
    for( int j = 0; j < array2.length; j++ )
    {
    if( array1[i] == array2[j] )
    {
    System.out.println("Intersection at: " + array1[i] );
    }
    }
    }
    }
    }


    // Output

    C:\>java test33
    Intersection at: 2
    Intersection at: 4

    C:\>


    Hope this helps,

    -Caitlin.
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2002
    Posts
    26
    Rep Power
    0
    hi,

    thnx for the reply.. had it solved a few days bak...

    cheers,
    sands

IMN logo majestic logo threadwatch logo seochat tools logo