DevOpsSauce
DevOpsSauce

Reputation: 1387

How to initialize an array to use in a test method later (Java)

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.

Upvotes: 0

Views: 734

Answers (3)

Jodn
Jodn

Reputation: 324

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.

Upvotes: 1

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

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.

Upvotes: 1

aleb2000
aleb2000

Reputation: 452

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];

Upvotes: 1

Related Questions