0

我有一个 LinkedList 的 ADT 的测试代码,它实现了接口 NumList.java,并在 NumLinkedList.java 中实现,并在 NumSet.java 中使用它。

我正在尝试使我的 NumSet 具有可以从双数组输入创建集合的方法,并使用拦截/联合和打印方法来使用和打印数据。

但我的测试代码显示我的测试 NumSet 值为空,即 testProof 和 testProof2。

所以现在我的 testProof 返回了一个空变量,这意味着没有任何东西保存在其中。

static public NumSet intersect(NumSet S1, NumSet S2) //check 2nd for and if//
{
    NumSet intersectAnswer = new NumSet();

    for (int i = 0; i < S1.set.size()-1; i++)
    {
        for(int j = 0; j < S2.set.size()-1; j++)
        {
            double FUZZ = 0.0001;
            if (Math.abs(S1.set.lookup(i) - S2.set.lookup(j)) < FUZZ) // double values, this is more precise than ==.
                {
                    intersectAnswer.set.insert(1, S1.set.lookup(i));
                }
        }
    }

    return intersectAnswer;

}

是testProof的方法,下面是定义testProof的地方。

public static void main(String[] args)
{
    double[] a = {1.3,2,3,4,101.9};
    double[] b = {3,7,13,901,-29.1,0.05};

    NumArrayList test;
    test = new NumArrayList();
    test.printTest(); //runs test code in NumList


    //ok below is running. what is wrong with intersect?
    NumSet test2;
    test2 = new NumSet(a);
    NumSet test4;
    test4 = new NumSet(b);
    NumSet testProof;
    NumSet testProof2;

    test2.print(); //print out test 2
    System.out.println();
    test4.print();
    System.out.println();
    testProof = intersect(test2,test4);

我已经初始化为

public class NumSet
{
private NumList set;

    public NumSet(double[] sth)
    {
        //moves elements of sth into set.
        set = new NumLinkedList();
        for(int i = 0; i < sth.length; i++)
        {
            set.insert(0,sth[i]);
        }
        set.removeDuplicates();
    }

    public NumSet()
    {
        set = new NumLinkedList();
    }

int numSet = 0;

我的拦截、联合和打印如下:

public NumSet intersect(NumSet S1, NumSet S2) //check 2nd for and if//
{
    NumSet intersectAnswer = new NumSet();

    for (int i = 0; i < S1.set.size()-1; i++)
    {
        for(int j = 0; j < S2.set.size()-1; j++)
        {
            if (S1.set.lookup(i) == S2.set.lookup(j))
                {
                intersectAnswer.set.insert(0, S1.set.lookup(i));
                }
        }
    }

    // intersectAnswer.set.removeDuplicates();  unnecessary, sets are already removed of duplicates
    return intersectAnswer;

}

public NumSet union(NumSet S1, NumSet S2)
{   //check logic.
    NumSet unionAnswer = new NumSet();

    for (int i = 1; i < S1.set.size()+1; i++)
    {
        unionAnswer.set.insert(1, S1.set.lookup(i));
    }

    for (int i = 1; i < S2.set.size()+1; i++)
    {
        unionAnswer.set.insert(1, S2.set.lookup(i)); 
    }
    unionAnswer.set.removeDuplicates();
    return unionAnswer;
}

public void print() 
{
    for (int i = 0; i < set.size()-1; i++)
    {
        System.out.print(set.lookup(i) + ",");

    }
    System.out.print(set.lookup(set.size()-1));
}

查找和大小是从我的 NumLinkedList.java 引用的,如下所示

public int size() // measure size of list by counting counter++;
{


    return nItem; 
}


public double lookup(int i) 
{
    if( i <0 || i >= size()) //cannot lookup nonexistant object
    {
        System.out.println("out of bounds " + i + " < 0 or > " + size() );
        //how do I break out of this loop?
        System.out.println("just returning 0 for the sake of the program");

        return 0;
    }

    if(i == 0)
    {
        return head.value;
    }

    double answer = 0;
    Node currNode = head;
    for(int j = 0; j < i+1; j++) //move to ith node and save value
    {
        answer = currNode.value;    
        currNode = currNode.next;   
    }

    return answer;
}

最后我的测试代码如下,其中 testProof 和 testProof2 是。

public static void main(String[] args)
{
    double[] a = {1.3,2,3,4,101.9};
    double[] b = {3,7,13,901,-29.1,0.05};

    NumArrayList test;
    test = new NumArrayList();
    test.printTest(); //runs test code in NumList


    //ok below is running. what is wrong with intersect?
    NumSet test2;
    test2 = new NumSet(a);
    NumSet test4;
    test4 = new NumSet(b);
    NumSet testProof;
    NumSet testProof2;

    test2.print();
    System.out.println();
    testProof = test2.intersect(test2, test4);
    System.out.println("tried intersect");
    testProof.print(); 
    System.out.println();
    System.out.println("tried test.print()");
    testProof2 = test2.union(test2,test4);
    System.out.println("tried union");
    testProof2.print(); 
    System.out.println();
    System.out.println("NumSet ran fully.");
4

1 回答 1

0

我建议您在调试时使用整数值而不是精度值来实现 NumSet 类,因为在此调试阶段比较两个双精度值往往会给您的代码增加一些不必要的复杂性。

您可能想查看您的removeDuplicates() 方法,我认为这可能会解决您的问题。不幸的是,我在您发布的代码中看不到它。

实际上, intersect() 方法中的这部分代码从一开始就注定会失败,

if (S1.set.lookup(i) == S2.set.lookup(j))

由于您使用双精度数, == 是比较两个不同值的一种非常不精确的方法,更好的方法是允许一定量的精度误差,即

double final FUZZ = 0.0001
if (Math.abs(S1.set.lookup(i) - S2.set.lookup(j)) < FUZZ )
    //...
于 2012-09-29T00:33:15.883 回答