2012-09-28 39 views
0

我有一個LinkedList的ADT的測試代碼,它實現了接口NumList.java,並在NumLinkedList.java中實現,並在NumSet.java中使用它。ADT LinkedList相交集錯誤

我試圖讓我的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."); 

回答

0

我建議你實現你NumSet類與當你調試,因爲比較兩個雙值趨向於一些不必要的複雜性,在此調試階段添加到您的代碼整數值而不是雙重價值

你可能想看看你的removeDuplicates()方法,我認爲這可能會解決你的問題。不幸的是,我沒有在你發佈的代碼中看到它。

實際上,這部分代碼的交叉()方法註定從一開始就失敗中,

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) 
    //... 
+0

哦,我不知道,==是如此不精確的,謝謝你的幫助 – Sukwoo

+0

權,所以現在我獲得價值 - 我改變了即時通訊將「set」複製到NumArrayList,這是一個ADT數組列表,並且我正在獲取union的值,但不會相交。我敢肯定,原因是代碼並沒有告訴程序將相交值存儲到我的testProof中。 – Sukwoo

+0

好吧,如果將NumList中的設置更改爲NumArrayList,那麼我猜測您的這兩個ADT的底層實現都是錯誤的。有沒有原因,你不使用內置到Java的?即,列表或ArrayList 。 – BillToWin