这里第一次。我的问题是,我的代码给我一个例外边界错误,我不明白为什么。我只需要代码来打印相交值而不重复。感谢您的帮助,如果您需要更多信息,请询问。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(" ");
}
}
检查堆栈跟踪并告诉我们发生故障的线路。 – 2014-09-01 16:41:15
错误发生在z [c] = x [i];和arrayTest(x,y); – 2014-09-01 16:46:01
在该行之前,打印出c和i。一个应该很奇怪。如果可以的话,使用调试器 - 这很容易。 – 2014-09-01 17:07:14