查找k个集合之间的共同元素。查找常见元素(字符串)时出错

时间:2018-09-26 17:14:57

标签: java arrays merge binary-search-tree insertion-sort

给出两个元素A和B的集合,生成第三个集合C,仅包含集合A和B中常见的那些元素,并且允许重复。

这是我应该遵循的标准:

它应该能够将以简单数组存储的k个集合的0作为输入。由于我们尚未介绍高阶数据结构,因此我们将数据结构限制为数组。

集合的元素都应为Comparable类型,并且都应从相同的基类派生(不计算Object类)。可比较接口的实现是必需的,因为必须将各个元素进行相互比较以确定通用性。它们必须都源自同一基类,因为未定义不同数据类型之间的比较。

应允许重复的元素;例如,如果在所有输入集合中有M个值“ XYZ”的实例,则在公共元素的集合中应该有M个值“ XYZ”的实例。 应允许收集的长度不等;也就是说,某些收藏可能比其他收藏更多。 必须将一个集合指定为“查询”集合,该集合是包含要与其他集合中的元素进行比较的元素的集合。 进行的元素比较的总数应小于上述二次求解的值。也就是说,最坏情况下的比较总数应小于(k-1)N2。不必担心平均性能或最佳案例性能。同样,对于此分配,将比较的总数定义为仅在遍历查询集合之后执行的那些比较,并检查其他集合中查询集合中元素的存在。在搜索公共元素之前进行的用于操纵数据的任何比较均应忽略。 这是我到目前为止用于CommonElements的代码:

package Module4;
// class
public class CommonElements<T> implements Comparable<T> {
    // my variable to see how many comparisons
    private int comparisons = 0;
    // the array i'm working with
    private Comparable[][] MyArray;
    // my constructor
    CommonElements() {
    }
    // my constructor and its assigned value
    CommonElements(Comparable[][] collections) {
        MyArray = collections;
}
  // my method, having trouble calling this  
public Comparable[] findCommonElements(Comparable[][] collections) {
    try {
        // call method
        insertionSort(collections);
}
    catch (NullPointerException e) {
        System.out.println("Error: Empty Array!");
}    //Define my array
    Comparable[] CommonCollection;

    if (collections.length > 0) {
        //creates a new instance
        CommonCollection = new Comparable[collections[0].length];
        Comparable[] QueryCollection = createQuery(collections);
        Comparable[][] SecondCollection = createOther(collections);
        int lFll = 0;
        //loop
        for (Comparable query : QueryCollection) {
            //Declare Variable
            boolean lMatch = true;
            //loop
            for (int i = 0; i < SecondCollection.length; i++) {

                for (int j = 0; j < SecondCollection[i].length; j++) {
                    //increment number of comparisons
                    comparisons++;
                    //if value matches then true
                    if (query.compareTo(SecondCollection[i][j]) == 0) {

                        lMatch = true;

                        break;
                        }
                    else
                        lMatch = false;
                    }
                }

            if (lMatch == true) {
                //assign value
                CommonCollection[lFll] = query;

                lFll++;
                }
            }
        }
    else 
    {
        CommonCollection = createQuery(collections);
        }

    return CommonCollection;
    }
//define method
public int getComparisons() {
    return comparisons;
}
//define method
public Comparable[][] getArray() {
    return MyArray;
}
//define method
public void lStArry(Comparable[][] collections) {
    MyArray = collections;
}
//define method
public Comparable[] createQuery(Comparable[][] collections) {
//create instance
    Comparable[] QueryCollection = new Comparable[collections.length];
    //loop
    for (int i = 0; i < collections[0].length; i++) {
        //assign value
        QueryCollection[i] = collections[0][i];
        }
    return QueryCollection;
    }
//define method
public Comparable[][] createOther(Comparable[][] collections) {
    Comparable[][] SecondCollection = new Comparable[collections.length - 1][];
    for (int i = 1; i < collections.length; i++) {
        for (int j = 0; j < collections[i].length; j++) {
            SecondCollection[i][j] = collections[i][j];
            }
        }
    return SecondCollection;
    }
//define method
public Comparable[][] insertionSort(Comparable[][] collections) {
    for (int j = 0; j < collections.length; j++) {
        for (int i = 1; i < collections[j].length; i++) {
            Comparable firstUnsorted = collections[j][i];
            int index = i - 1;

            while (index >= 0 && firstUnsorted.compareTo(collections[j][index]) < 0) {
                collections[j][index + 1] = collections[j][index];

                index--;
                }
            collections[j][index + 1] = firstUnsorted;
            }
        }
    return collections;
    }
//define method
public void toString(Comparable[][] collections) {
    for (int i = 0; i < collections.length; i++) {
        for (int j = 0; j < collections[i].length; j++) {
            System.out.println("Set " + i + ": " + collections[i][j]);
            }
        }
    }
//define method
public void toString(Comparable[] collections) {
    for (int i = 0; i < collections.length; i++) {
        System.out.println("Set " + i + ": " + collections[i]);
        }
    }

//Override

@Override

//Define a method

public int compareTo(T other) {
    if (this.compareTo(other) < 0 || this.compareTo(other) > 0)
        return Integer.signum(this.compareTo(other));
    else
        return 0;
    }
}

以下是我提供给我的数组的主要内容:

package Module4;


public class Module4{


public static void main(String[] agrs){

    CommonElements<String> MyArray = new CommonElements<>();

    Comparable[] col_1 = {"A"};          
    Comparable[] col_2 = {"A", "B"};
    Comparable[] col_3 = {"A","B","C"};
    Comparable[][] collections = {col_1, col_2, col_3};

MyArray.findCommonElements(collections); 

System.out.println(MyArray.findCommonElements(collections));
}
}

我的教授给了我们一个指导性模板,该模板显示:1.创建一个名为CommonElements的类,以包含您的算法以及相关的方法和属性。 2.在CommonElements类中,将算法封装在一个名为findCommonElements的方法中,该方法具有以下签名:public Comparable [] findCommonElements(Comparable [] [] collections)。

我使用上面的错误是:

Exception in thread "main" java.lang.NullPointerException
    at Module4.CommonElements.createOther(CommonElements.java:97)
    at Module4.CommonElements.findCommonElements(CommonElements.java:30)
    at Module4.Module4.main(Module4.java:16)

Java 97是:

public Comparable[][] createOther(Comparable[][] collections) {
    Comparable[][] SecondCollection = new Comparable[collections.length - 1][];
    for (int i = 1; i < collections.length; i++) {
        for (int j = 0; j < collections[i].length; j++) {
            **SecondCollection[i][j] = collections[i][j];**
            }
        }
    return SecondCollection;
    }

Java 30是:

if (collections.length > 0) {
        //creates a new instance
        CommonCollection = new Comparable[collections[0].length];
        Comparable[] QueryCollection = createQuery(collections);
        **Comparable[][] SecondCollection = createOther(collections);**
        int lFll = 0;

最后是Java 16

MyArray.findCommonElements(collections);

我看不出为什么我不使用for循环进行编译...有什么建议吗?

0 个答案:

没有答案