2014-01-28 95 views
0

當我跑,我得到這樣的:計算陣列交匯的獨特元素的數量

運行assignment1question1

應該回到「2」,因爲我試圖找到兩個數組相交中唯一元素的數量。請幫忙。 謝謝。

public class assignment1question1 { 
    public static void main(String[] args) { 
     int[] a = {1,3,2,3,5}; 
     int[] b = {1,3,4,1,7,3}; 
     int n = 5; 
     int m = 6; 
     System.out.print(listIntersection(a,b,n,m)); 
    } 
    public static int listIntersection (int[] a, int[] b, int n, int m) { 
     int i,j,k; 
     int intersect = 0; 

     for(i=0; i<n; i++) { 
      int duplicate = 0; 

      for(j=0; j<=i; j++) { 
       if(a[i] == a[j]) { 
        duplicate = duplicate + 1; 
       } 
      } 
      if(duplicate == 1) { 
       for(k=0; k<m; m++) { 
        if(a[i] == b[k]) { 
         intersect = intersect + 1; 
        } 
       } 
      } 
     } 
    return intersect; 
    } 
} 

這是我更新的代碼:

public class assignment1question1 { 
    public static void main(String[] args) { 
    int[] a = {1,3,2,3,5}; 
    int[] b = {1,3,4,1,7,3}; 
    int n = a.length; 
    int m = b.length; 
    System.out.print(listIntersection(a,b,n,m)); 
    } 
    public static int listIntersection (int[] a, int[] b, int n, int m) { 
     int i,j,k; 
     int intersect = 0; 

     for(i=0; i<n; i++) { 
     int duplicate = 0; 

     for(j=1; j<=i; j++) { 
      if(a[i] == a[j]) { 
      duplicate = duplicate + 1; 
     } 
     } 
     if(duplicate == 0) { 
      for(k=0; k<m; k++) { 
      if(a[i] == b[k]) { 
       intersect = intersect + 1; 
       break; 
      } 
      } 
     } 
     } 
    return intersect; 
    } 
    } 
+1

爲什麼你'N'和'M'而不是使用'a.length'和'b.length'? –

+1

2147483642 *在Java中幾乎是* int(它是帶符號的)的最大值。所以有理由認爲你的'intersect = intersect + 1'語句被重複執行,直到它不能再被增加。嘗試從內到外調查什麼會導致這種情況。 –

+1

在最後一個for循環中,您正在增加「m」,必須增加「k」 –

回答

0

如果你想讓你的代碼工作,你將需要修復k for-cycle,在那裏你迭代b數組。

  if(duplicate==1) { 
       for(k=0; k<m; k++) { // increase k 
        if(a[i] == b[k]) { 
         intersect = intersect + 1; 
         break; // insert break to avoid duplicates in b array 
        } 
       } 
      } 

而且這裏有點清潔和更短的代碼。 (我想,你是不是允許使用Java集合類。)

public class assignment1question1 { 

    public static void main(String[] args) { 
     int[] a = {1,3,4,3,5}; 
     int[] b = {1,3,4,1,7,3}; 
     System.out.print(listIntersection(a,b)); 
    } 

    public static int listIntersection(int[] a, int[] b) { 
     int intersect = 0; 

     for(int i = 0; i < a.length; i++) { 
      boolean duplicate = false; 

      for (int j = 0; j < i; j++) { 
       if(a[i] == a[j]) { 
        duplicate = true; 
        break; 
       } 
      } 

      if (!duplicate) { 
       for(int k = 0; k < b.length; k++) { 
        if (a[i] == b[k]) { 
         intersect++; 
         break; 
        } 
       } 
      } 
     } 
     return intersect; 
    } 
} 
+0

找到問題,這是真的,我可以寫從j = 1開始,而不是0,然後使條件複製== 0. – Silent

+0

我錯了。你需要修理你的'k'形式。 –

+0

不,我認爲你是對的。我碰巧做出了與您展示的相同的修改。我現在的代碼是? – Silent

0

你被卡在無限循環becoz你增加M值是M +:

if(duplicate == 1) { 
        for(k=0; k<m; k++) { 
         if(a[i] == b[k]) { 
          intersect = intersect + 1; 
         } 
        } 
       } 
+0

完整的錯字...該死的。謝謝。意思是寫k ++ – Silent

+0

@ user3245760是的,它似乎。通過添加日誌 – Kick

0
import java.util.HashSet; 
import java.util.Set; 

public class Del { 
    public static void main(String[] args) { 
     int[] a = { 1, 3, 2, 3, 5 }; 
     int[] b = { 1, 3, 4, 1, 7, 3 }; 

     // duplicates removed by SET 
     Set intersect = new HashSet(); 

     for (int aVal : a) { 
      for (int bVal : b) { 
       if (aVal == bVal) { 
        intersect.add(aVal); 
        System.out.println("->" + aVal); 
       } 
      } 
     } 
     System.out.println("Size =" + intersect.size()); 
    } 
} 
0
import java.util.*; 

public class HelloWorld { 

    public static void main(String []args) { 
     int[] a = {1,3,2,3,5}; 
     int[] b = {1,3,4,1,7,3}; 
     Set<Integer> aTmp = asSet(a); 
     aTmp.retainAll(asSet(b)); 
     System.out.println(aTmp.size()); 
    } 

    public static Set<Integer> asSet(int... args) { 
     Set<Integer> tmp = new HashSet<Integer>(); 
     for (int i : args) { 
      tmp.add(i); 
     } 
     return tmp; 
    } 
}