0

I'm comparing elements in two arrays and printing those elements, plus the number of comparisons.

In the "findCommonElements" method, I can't have empty arrays, or I get a NullPointerException (of course) error.

I can't just declare them, or I get the "variable may have not been initialized" error.

I have to use multiple versions of these arrays for testing, but I'm not sure how to go about this. The program is only reading the first instances of the arrays.

Code:

import java.util.*;

public class CommonElements {

private static int comparisons = 0;

public static void main(String[] args) {

    new CommonElements().firstTest();
    //new CommonElements().secondTest();
    //new CommonElements().thirdTest();
}
public void setComparisons(int comparisons) {
    this.comparisons = comparisons;
}
public int getComparisons() {
    return comparisons;
}
public static Comparable[] findCommonElements(Comparable[][] collections) {

    Comparable[] arr1 = {'A', 'B', 'C', 'D'};
    Comparable[] arr2 = {'C', 'D', 'E', 'F', 'G'};
    Comparable[] hashArray;
    Comparable[] searchArray;
    if(arr1.length < arr2.length) {
        hashArray = arr1;
        searchArray = arr2;
    }
    else {
        hashArray = arr2;
        searchArray = arr1;
    }
    HashSet<Comparable> intersection = new HashSet<>();
    HashSet<Comparable> hashedArray = new HashSet<>();
    for(Comparable element : hashArray) {
        hashedArray.add(element);
    }
    for(Comparable element : searchArray) {
        if(hashedArray.contains(element)) {
            intersection.add(element);
            comparisons++;
        }
    }
    return intersection.toArray(new Comparable[0]);
}
public void firstTest() {
    Comparable[] array1 = {'A', 'B', 'C', 'D', 'E'};
    Comparable[] array2 = {'E', 'F', 'G', 'H'};
    Comparable[][] collections = {array1, array2};
    Comparable[] commonStuff = CommonElements.findCommonElements(collections);
    System.out.println("First Test");
    System.out.println("Comparisons: " + getComparisons());
    System.out.print("Common Elements: ");
    for(Comparable element : commonStuff) {
        System.out.print(element.toString() + " ");
    }
    System.out.println();
}

/*
public void secondTest() {
    Comparable[] array1 = {'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'};
    Comparable[] array2 = {'D', 'E', 'F', 'G', 'H'};
    Comparable[][] collections = {array1, array2};
    Comparable[] commonStuff = findCommonElements(collections);
    System.out.println("Second Test");
    System.out.println("Comparisons: " + getComparisons());
    for(Comparable element : commonStuff) {
        System.out.print(element.toString() + " ");
    }
    System.out.println();
}
public void thirdTest() {
    Comparable[] array1 = {'1', '2', '3', '3'};
    Comparable[] array2 = {'3', '2', '4', '5', '6'};
    Comparable[][] collections = {array1, array2};
    Comparable[] commonStuff = findCommonElements(collections);
    System.out.println("Third Test");
    System.out.println("Comparisons: " + getComparisons());
    for(Comparable element : commonStuff) {
        System.out.print(element.toString() + " ");
    }
    System.out.println();
}
*/
}

This is the output, and it's reading the arrays in the findCommonElements method, rather than the firstTest() method (The other test methods are identical, except for the values in the arrays, so they're commented out):

    First Test
    Comparisons: 2
    Common Elements: C D

NOTE Yes, I have an unused parameter in the findCommonElements method. It was a requirement, and I didn't understand how to incorporate the 2D array into this.....at least not yet.

Yes, the arrays are of type Comparable. That's another requirement.

Also, if any of my logic is incorrect in this code, my apologies. I'm just trying to figure this one problem out for now.

3 Answers 3

1

Instead of

public static Comparable[] findCommonElements(Comparable[][] collections) {
  Comparable[] arr1 = {'A', 'B', 'C', 'D'};
  Comparable[] arr2 = {'C', 'D', 'E', 'F', 'G'};

Write this (assuming you always get exactly 2 elements):

public static Comparable[] findCommonElements(Comparable[][] collections) {
  Comparable[] arr1 = collections[0];
  Comparable[] arr2 = collections[1];

Then your tests start to work.

Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! That's where I was missing the logic of incorporating the 2D array!
1

To declare an array you should use this syntax:

// Type[] array = new Type[arrayLenght];

For example if you want to create an array of Comparable of 10 lenght you should do something like this:

Comparable[] myArray = new Comparable[10];

Comments

1

You can check your parameter for null and if any of it is null you can return an empty Comparablearray (which is a true thing in an empty case).

  public static Comparable[] findCommonElements(Comparable[][] collections) {
    if((collections[0]==null) || (collections[1]==null)){
    return new Comparable[0];
    }
    Comparable[] arr1 = collections[0];
    Comparable[] arr2 = collections[1];
    Comparable[] hashArray;
    Comparable[] searchArray;
    if(arr1.length < arr2.length) {
    [...]

Btw. since you have a field for comparisons. Make sure you handle it right. Set it to 0 whenever you start a new findCommonElements or something else.

3 Comments

Thanks. I just noticed that upon running the other test methods. My comparisons are way off.
I reset the variable comparisons to 0 at the beginning of the secondTest() and thirdTest() methods, if that's the right way to go about it. The expected output of the firstTest() is 1 comparison, and one element returned ("E"). The second should be 5 with "D E F G H", and third with 2 and "2 3", in which case they all display that.
Now, incorporating some Data Structures/Algorithms in this, with the comparisons being the same as the returned common elements, does this mean I'm achieving O(n) time complexity?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.