2014-09-01 49 views
0

這裏第一次。我的問題是,我的代碼給我一個例外邊界錯誤,我不明白爲什麼。我只需要代碼來打印相交值而不重複。感謝您的幫助,如果您需要更多信息,請詢問。2個隨機數組的交集 - Java

我不能使用任何數字,但陣列,無哈希或陣列列表。

下面是代碼:

import java.util.Random; 

public class ArraySort 
{ 

    static int i,j,k,c=0; 
    public static void main (String [] args) 
    { 
     int [] x = new int [50]; 
     Random generator = new Random(); 
     System.out.println("Values in array X:"); 
     for (int i = 0; i < x.length; i++){ 
      x[i] = generator.nextInt(20); 
      // count = count + 1; 
      System.out.print(x[i]+" "); 
     } 
     System.out.println(""); 
     int [] y = new int [50]; 
     System.out.println("Values in array Y:"+" "); 
     for(int j =0; j < y.length; j++){ 

      y[j] = generator.nextInt(20); 
      // count1 = count1 + 1; 
      System.out.print(y[j]+" "); 
     } 
     System.out.println(""); 
     arrayTest(x,y); 
    } 

    public static void arrayTest (int x [] , int y[]) 
    { 
     int [] z = new int [50]; 
     // int [] b = new int [50]; 

     for(i = 0; i < (x.length); i++) 
     { 
      for (j = 0; j <y.length; j++) 
      { 

       if (x[i] == y[j]) 
       { 

        z[c]=x[i]; 

        c++; 
       } 
       else 
        continue; 
      } 

     } 

     System.out.println("Values in array A:"); 
     for(k =0; k < c; k++) 
     { 
      System.out.print(" "+z[k]+" "); 
     } 
     System.out.println("   "); 
    } 

} 
+0

檢查堆棧跟蹤並告訴我們發生故障的線路。 – 2014-09-01 16:41:15

+0

錯誤發生在z [c] = x [i];和arrayTest(x,y); – 2014-09-01 16:46:01

+0

在該行之前,打印出c和i。一個應該很奇怪。如果可以的話,使用調試器 - 這很容易。 – 2014-09-01 17:07:14

回答

1

您的代碼不處理重複的,這意味着如果兩個數組有50個相同的數字,你會試圖插入50 * 50的輸出數組,其大小僅50

爲了解決這個問題,可以維護一個包含所有添加到z陣列的數字的,並增加了新的號碼z之前,檢查它不是已經在這集。

想一想,把交叉點放在一個Set而不是一個數組中會更好,因爲你不知道有多少個元素在裏面,所以使用一個數組(它有一個固定的長度)意義不大。如果你必須的話,你可以從最後的Set中創建一個輸出數組。

而不類別中的溶液,其依賴於值xy的有限範圍可容納:

public static void arrayTest (int x [] , int y[]) 
{ 
    boolean[] z = new boolean[20]; 
    for(i = 0; i < (x.length); i++) { 
     for (j = 0; j <y.length; j++) { 
      if (x[i] == y[j]) { 
       z[x[i]]=true; 
      } 
     } 
    } 

    System.out.println("Values in intersection:"); 
    for(k =0; k < z.length; k++) { 
     if (z[k]) { 
      System.out.print(" " + k + " "); 
     } 
    } 
    System.out.println("   "); 
} 
+0

我的老師說我不能使用除數組以外的任何東西,所以沒有列表或哈希集。對於所有這些,我都是超新的 – 2014-09-01 16:44:43

+0

@WisdomOrji在這種情況下,可以依賴數組中所有數字都在0到19之間的事實。'z'可以是一個包含20個布爾值的數組,當您發現x [i] == y [j],將z [x [i]]設置爲true。 – Eran 2014-09-01 16:47:48

+0

是不是z [x [i]]將x [i]的內容設置爲z的位置?所以像x [i]會= 10那麼z [x [i]]會是z [10],還是我在數學上思考呢? – 2014-09-01 16:53:13

2

下面的代碼將處理重複太:

public static void main(String[] args) { 
     int a[] = {3, 10, 4, 2, 8}; 
     int[] b = {10, 4, 12, 3, 23, 1, 8}; 
     int[] c = new int[(int)Math.min(a.length, b.length)]; 
     int i=0; 
     for(int f=0;f<a.length;f++){ 
       for(int k=0;k<b.length;k++){ 
        if((a[f]==b[k]) && (doesArrayContainElement(c,a[f]) == false)) { 
        c[i] = a[f]; 
        i++; 
      } 
      } 
     } 
     for (int x=0; x<i; x++){ 
      System.out.println(c[x]); 
     } 
     } 
     public static boolean doesArrayContainElement(int array[], int element) { 
      for (int i=0; i<array.length; i++){ 
      if(array[i] == element) { 
       return true; 
      } 
      } 
     return false; 
     } 
    } 
+0

[(int)Math.min(a.length,b.length)]做什麼? – 2014-09-01 16:49:31

+0

它聲明'int'數組''c'的數組'a'或'b'的大小,這取決於它們中哪些具有最小大小。 – 2014-09-01 16:50:48

+0

這是如何處理重複的?它與問題中的代碼相同(數組的初始化除外)。如果[]會是{3,10,4,4,8},那麼4會被添加兩次到輸出數組。 – Eran 2014-09-01 16:56:27

0
/* package whatever; // don't place package name! */ 

import java.util.*; 
import java.lang.*; 
import java.io.*; 

/* Name of the class has to be "Main" only if the class is public. */ 
class Ideone 
{ 
    public static void display(int A[], int n){ 
     for(int i=0;i<n;i++) 
     System.out.print(A[i]+" "); 
     System.out.println(); 
    } 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     Random r = new Random(); 
     HashSet hs = new HashSet(); 
     int A[] = new int[10]; 
     int B[] = new int[10]; 
     int c[] = new int[20],ctr=0; 
     for(int i=0;i<10;i++){ 
      A[i] = r.nextInt(20); 
      hs.add(A[i]); 
      B[i] = r.nextInt(20); 
     } 
     display(A,10); 
     display(B,10); 
     for(int i=0;i<10;i++){ 
      if(hs.contains(B[i])) 
      {c[ctr] = B[i];ctr++;} 
     } 
     display(c,ctr); 

    } 
} 
+0

我不能使用HashSet,也不知道那是什麼。 – 2014-09-01 17:08:12