Java 比较两个数组中的公共项,并返回另一个数组中的公共项

Java 比较两个数组中的公共项,并返回另一个数组中的公共项,java,arrays,elements,items,Java,Arrays,Elements,Items,所以问题是我有两个数组,必须检查它们是否有公共项。通常的东西,非常简单。但对我来说,棘手的是,我必须返回另一个数组,其中包含已发现的公共元素。我不能使用任何集合。提前谢了。这是我到目前为止的代码 public class checkArrayItems { static int[] array1 = { 4, 5, 6, 7, 8 }; static int[] array2 = { 1, 2, 3, 4, 5 }; public static void main(St

所以问题是我有两个数组,必须检查它们是否有公共项。通常的东西,非常简单。但对我来说,棘手的是,我必须返回另一个数组,其中包含已发现的公共元素。我不能使用任何集合。提前谢了。这是我到目前为止的代码

public class checkArrayItems {
    static int[] array1 = { 4, 5, 6, 7, 8 };
    static int[] array2 = { 1, 2, 3, 4, 5 };

    public static void main(String[] args) {
        checkArrayItems obj = new checkArrayItems();
        System.out.println(obj.checkArr(array1, array2));

    }

    int[] checkArr(int[] arr1, int[] arr2) {
        int[] arr = new int[array1.length];
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) {
                    arr[i] = arr1[i];
                }

            }
        }
        return arr;

    }

}
公共类checkArrayItems{
静态int[]数组1={4,5,6,7,8};
静态int[]array2={1,2,3,4,5};
公共静态void main(字符串[]args){
checkArrayItems obj=新的checkArrayItems();
系统输出打印LN(对象检查arr(array1,array2));
}
int[]checkArr(int[]arr1,int[]arr2){
int[]arr=新的int[array1.length];
for(int i=0;i
我懒得输入代码,但这里是算法。 1.对两个数组进行排序 2.迭代数组,比较项目并增加索引


希望这有帮助。

在两个for循环之前声明一个索引

int index = 0;
它将保持arr阵列的当前位置。然后:

arr[index++] = arr1[i];

而且,由于您使用arr1.length初始化arr,因此在非冲突结束时,您的数组将被0填充。

您可以使用
arr[i]=Integer.MIN\u值为新数组
arr
中的元素使用最小或最大默认伪值。这样,您将能够区分真实值和虚拟值。如下图所示:

int[] checkArr(int[] arr1, int[] arr2) {
        int[] arr = new int[array1.length];
        for (int i = 0; i < arr1.length; i++) {
            arr[i] = Integer.MIN_VALUE;
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i] == arr2[j]) {
                    arr[i] = arr1[i];
                }

            }
        }
        return arr;

    }

建议:遵循类的命名约定,即制作
checkArrayItems
checkArrayItems

,以防有人想知道@user3438137提到的“追逐”算法是什么样子的:

int[] sorted1 = Arrays.copyOf(array1, array1.length);
Arrays.sort(sorted1);
int[] sorted2 = Arrays.copyOf(array2, array2.length);
Arrays.sort(sorted2);
int[] common = new int[Math.min(sorted1.length, sorted2.length)];
int numCommonElements = 0, firstIndex = 0; secondIndex = 0;
while (firstIndex < sorted1.length && secondIndex < sorted2.length) {
    if (sorted1[firstIndex] < sorted2[secondIndex]) firstIndex++;
    else if (sorted1[firstIndex] == sorted2[secondIndex]) {
        common[numCommonElements] = sorted1[firstIndex];
        numCommonElements++;
        firstIndex++;
        secondIndex++;
    }
    else secondIndex++;
}
// optionally trim the commonElements array to numCommonElements size
int[]sorted1=Arrays.copyOf(array1,array1.length);
数组。排序(sorted1);
int[]sorted2=Arrays.copyOf(array2,array2.length);
数组。排序(sorted2);
int[]common=new int[Math.min(sorted1.length,sorted2.length)];
int numcommonements=0,firstIndex=0;第二指数=0;
while(第一个索引
转换为列表并使用retainAll:@canillas
不能使用任何集合
因此使用2个循环,迭代并比较。使用谷歌。。。有很多这样的例子。@canillas我正在谷歌上搜索,但是没有任何3个数组的例子@ССцццПцааааааааааааааааааааа1072(每个都有)。@Eugene:不,这与二进制搜索无关。如果使用简单的算法,则必须将数组a的每个元素与数组B的每个元素进行比较,使算法为二次型。如果先对数组排序,则可以保留两个数组的当前索引(从a开始)并使用“追踪”使用较小的当前元素增加数组索引的算法,直到得到一个大于或等于另一个数组中当前元素的元素(如果元素相等,则将其添加到“公共元素”数组)。@Eugene:后一种算法主要由排序复杂度(O(max(n,m)log max(n,m))决定),因为“追逐”算法本身与两个数组大小之和是线性的。为此,他可以使用默认值,如
Integer.MIN_value
即第一个循环中不常见的值。你是对的,忘记了
Arrays.sort()
已就位,已更正。
通用
阵列的最大大小将为
min(sorted1.length,sorted2.length)
。你分配的内存太多了。@PiotrWilkin我想OP说过不要使用
Collection
。尽管我不确定
数组能否被称为
Collection
数组
只是一个包含处理数组的函数的实用类。根据定义,集合是从数组继承的东西
集合
(请不要要求我从头开始编写合并排序函数)你是如何迭代这个数组的。无论我如何尝试,我总是会返回一个hashCode?很抱歉这个愚蠢的问题,你是说从
checkArr
方法返回后的
arr
?这些值是Integer.MIN\u值返回的值。代码很完美,只是我不知道如何打印出你上面给出的结果。很抱歉is:)@Саааааааааа现在检查
int[] sorted1 = Arrays.copyOf(array1, array1.length);
Arrays.sort(sorted1);
int[] sorted2 = Arrays.copyOf(array2, array2.length);
Arrays.sort(sorted2);
int[] common = new int[Math.min(sorted1.length, sorted2.length)];
int numCommonElements = 0, firstIndex = 0; secondIndex = 0;
while (firstIndex < sorted1.length && secondIndex < sorted2.length) {
    if (sorted1[firstIndex] < sorted2[secondIndex]) firstIndex++;
    else if (sorted1[firstIndex] == sorted2[secondIndex]) {
        common[numCommonElements] = sorted1[firstIndex];
        numCommonElements++;
        firstIndex++;
        secondIndex++;
    }
    else secondIndex++;
}
// optionally trim the commonElements array to numCommonElements size