public class Test { public static void main(String[] args) { printAll(intersection(new int[]{-5} ,new int[]{6,55,66,-4,-5} ) ); } static int [] intersection(int array1[] , int array2[]){ java.util.ArrayList<Integer> arrayList = new java.util.ArrayList<Integer>(); for(int outer=0; outer< array1.length ; outer++){ for(int inner=0; inner < array2.length ; inner++){ if(array1[outer] == array2[inner]){ arrayList.add(array1[outer]); // or arrayList.add(array2[outer]); will do } } } int intersectionArray[] = new int[arrayList.size()]; // now add all the elements in arrayList to intersectionArray[] for(int i = 0 ; i < arrayList.size() ; i++){ intersectionArray[i] = arrayList.get(i); } return intersectionArray; } static void printAll(int array[]){ for(int i =0 ; i < array.length ; i++){ System.out.println(array[i]); } } }
Wednesday, June 22, 2011
intersection of two int arrays - java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment