Compile common elements from multiple collections in Java -
this question has answer here:
i attempting traverse multiple collections , copy common elements (including duplicates) collections collection of common elements. have keep tally of number of comparisons performed collections. have created 3 test cases well. however, have run few problems along way.
1.) cannot common list show duplicates, or common elements matter.
2.) getting arrayindexoutofboundsexception after "completes" first test case
here output receive:
test 1 42 [a, c, e, f, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null] exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 @ commonelements.findcommonelements(commonelements.java:22) @ commonelements.test2(commonelements.java:69) @ commonelements.main(commonelements.java:90)
here code far:
import java.util.arrays; public class commonelements { private int comparisons = 0; public comparable[] findcommonelements(object[] collections) { int i, j, k = collections.length, l, items = 0; comparable[] common = new comparable[20]; comparable[] query = (comparable[]) collections[0]; comparable[] search = (comparable[]) collections[k - 1]; (j = 0; j < query.length; j++) { l = 0; (i = 0; < search.length; i++) { setcomparisons(getcomparisons() + 1); comparable searchobj = query[l]; if (l < query.length) { if (search[j].compareto(searchobj) == 0) { common[items] = searchobj; items++; } } l++; } k--; if (k > 0) { j = 0; = 0; l = 0; search = (comparable[]) collections[k]; } } return common; } public int getcomparisons() { return comparisons; } public void setcomparisons(int comparisons) { this.comparisons = comparisons; } public void test1() { comparable[] items1 = {'a', 'b', 'c', 'd', 'e', 'f'}; comparable[] items2 = {'d', 'e', 'f', 'g', 'h', 'i'}; comparable[] items3 = {'a', 'c', 'e', 'g', 'i', 'k'}; object[] collection = {items1, items2, items3}; comparable[] result = findcommonelements(collection); system.out.println("test 1"); system.out.println(getcomparisons()); system.out.println(arrays.tostring(result)); system.out.println(); //expected output: [a,c,d,e,e,f] } public void test2() { comparable[] items1 = {'a', 'b', 'c', 'd', 'e', 'f'}; comparable[] items2 = {'d', 'e', 'f', 'g', 'h'}; comparable[] items3 = {'a', 'c', 'e', 'g'}; object[] collection = {items1, items2, items3}; comparable[] result = findcommonelements(collection); system.out.println("test 2"); system.out.println(getcomparisons()); system.out.println(arrays.tostring(result)); system.out.println(); //expected output: [a,c,e,e] } public void test3() { comparable[] items1 = {'a', 'b', 'c', 'd'}; comparable[] items2 = {'d', 'e', 'f', 'g', 'h'}; comparable[] items3 = {'a', 'c', 'e', 'g', 'i', 'k'}; object[] collection = {items1, items2, items3}; comparable[] result = findcommonelements(collection); system.out.println("test 3"); system.out.println(getcomparisons()); system.out.println(arrays.tostring(result)); system.out.println(); //expected output: [a,c,d] } public static void main(string[] args) { new commonelements().test1(); new commonelements().test2(); new commonelements().test3(); } }
i know hot mess, appreciated. thank in advance.
public class commonelements { private int comparisons = 0; public <t extends comparable> list<t> findcommonelements(t[][] arrays) { final list<t> commonelements = new arraylist<>(); // find shortest array minimise number of comparisons t[] shortestarray = null; (t[] array : arrays) { if (shortestarray == null || array.length < shortestarray.length) shortestarray = array; } if (shortestarray == null) return collections.emptylist(); (t element : shortestarray) { boolean common = true; (t[] array : arrays) { // not compare on if (array == shortestarray) continue; boolean found = false; (int j = 0; j < array.length; ++j) { final t compareelement = array[j]; if (compareelement != null) { comparisons++; if (compareelement.equals(element)) { array[j] = null; found = true; break; } } } common = found; // if not found on array, no need check remaining ones if (!found) break; } if (common) commonelements.add(element); } return commonelements; } public int getcomparisons() { return comparisons; } public void setcomparisons(int comparisons) { this.comparisons = comparisons; } public void test1() { comparable[] items1 = {'a', 'b', 'c', 'd', 'e', 'f'}; comparable[] items2 = {'d', 'e', 'f', 'g', 'h', 'i'}; comparable[] items3 = {'a', 'c', 'e', 'g', 'i', 'k'}; comparable[][] collection = {items1, items2, items3}; list<comparable> result = findcommonelements(collection); system.out.println("test 1"); system.out.println(getcomparisons()); system.out.println(result); system.out.println(); } public void test2() { comparable[] items1 = {'a', 'b', 'c', 'd', 'e', 'f'}; comparable[] items2 = {'d', 'e', 'f', 'g', 'h'}; comparable[] items3 = {'a', 'c', 'e', 'g'}; comparable[][] collection = {items1, items2, items3}; list<comparable> result = findcommonelements(collection); system.out.println("test 2"); system.out.println(getcomparisons()); system.out.println(result); system.out.println(); } public void test3() { comparable[] items1 = {'a', 'b', 'c', 'd'}; comparable[] items2 = {'d', 'e', 'f', 'g', 'h'}; comparable[] items3 = {'a', 'c', 'e', 'g', 'i', 'k'}; comparable[][] collection = {items1, items2, items3}; list<comparable> result = findcommonelements(collection); system.out.println("test 3"); system.out.println(getcomparisons()); system.out.println(result); system.out.println(); } public static void main(string[] args) { new commonelements().test1(); new commonelements().test2(); new commonelements().test3(); } }
Comments
Post a Comment