2014-09-01 45 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