public class Test {
public static void main(String[] args) {
Integer[] coll_1 = {1, 2, 4, 6, 10, 12, 13, 14, 15, 20};
Integer[] coll_2 = {4, 6, 8, 9, 10, 11, 15, 19, 21, 23};
Object[] collections = new Object[2];
collections[0] = coll_1;
collections[1] = coll_2;
CommonElements.findCommonElements(collections);
}
}
public class CommonElements implements Comparable<Integer>{
private static int comparisons = 0;
private static Integer[] common = {};
public int getComparisons() {
return comparisons;
}
public static Comparable[] findCommonElements(Object[] collections)
{
Integer[] query = {1, 2, 3, 4, 9, 20, 21, 30, 31, 35 };
int i;
for( i = 0; i < collections.length; i++ ){
comparisons++;
if(query.equals( collections[i] )) {
common = (Integer[]) collections[i];
}
}
System.out.println("The following elements were common among all arrays: " + "\n");
System.out.println( common);
System.out.println("\n" + "Additionally, there were a total of " + comparisons + " comparisons made between the arrays." + "\n");
return common;
}
public int compareTo(Integer collections) {
return 0;
}
}
Output:
The following elements were common among all arrays:
[Ljava.lang.Integer;@c21495
Additionally, there were a total of 2 comparisons made between the arrays.

New Topic/Question
Reply



MultiQuote






|