dimanche 21 février 2016

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.

Aucun commentaire:

Enregistrer un commentaire