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

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Number of comparisons


    Ok, can anyone point out the exact location(s) of my errors. I'm getting the appropriate common elements, but the right number of comparisons.


    public class Test {


    public static void main(String[] args) {

    Integer[] collection1 = {1, 2, 3, 4, 5, 16};
    Integer[] collection2 = {1, 2, 3, 4, 16, 21};
    Integer[] collection3 = {1, 2, 3, 4, 5, 6, 7, 8};
    Integer[] collection4 = {1, 2, 3, 4, 6, 21};
    Object[] storeAllArray = {collection1, collection2, collection3};
    System.out.println(CommonElements.findCommonElements(storeAllArray));
    System.out.println("Comparison: " + CommonElements.getComparisons());



    }
    }

    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;


    public class CommonElements {

    public static int comparisons = 0;
    public static Object[] collections;
    public static Object[] common = new Comparable[20];
    public int count = 0;

    public static int getComparisons()
    {
    return comparisons;
    }
    public void setComparisons(int comparisons)
    {
    CommonElements.comparisons = comparisons;
    }
    public static Comparable<?> getNumber()
    {
    return getNumber();
    }

    public static int getIndexOfLowest(Object[] collections)
    {
    int indexOfLowest = 0;
    int size = 0;
    for(int i = 0; i < collections.length; i++)
    {
    Arrays.sort((Comparable[])collections[i]);
    size = ((Comparable[])collections[i]).length - 1;
    if(size < ((Comparable[])collections[indexOfLowest]).length)
    {
    indexOfLowest = i;
    }
    }
    System.out.println("The common elements in the arrays are: " );
    return indexOfLowest;
    }


    public static Comparable[] findCommonElements(Object[] collections)
    {
    int number = getIndexOfLowest(collections);
    Set<Object> commonElements = new HashSet<Object>();
    for(int i = 0; i < collections.length; i++)
    {
    Set<Object> hashSet = new HashSet<Object>(Arrays.asList(collections[number]));
    comparisons++;
    if(hashSet.contains(collections[i]))
    {
    commonElements.add(collections[i]);
    }
    }
    Object[] collection = commonElements.toArray(new Object[0]);
    System.out.println(Arrays.deepToString(collection));
    return (Comparable[]) common;
    }


    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    Not the right number of comparisons
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0
    Sorry for the formatting, posting from my phone.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please edit the post and wrap the code in code tags.

    Can you explain what the problem is? Post the program's output and add some comments saying what is wrong with it and show what it should be,

IMN logo majestic logo threadwatch logo seochat tools logo