【问题标题】:compare two arrays for common Items and return another array with the common items比较两个数组的公共项并返回另一个具有公共项的数组
【发布时间】:2017-11-16 11:40:14
【问题描述】:

所以问题是我有两个数组,必须检查它们是否有共同的项目。通常的东西,很容易。但对我来说棘手的是我必须返回另一个数组,其中包含已发现常见的元素.我不能不使用任何收藏。提前致谢。这是我目前的代码!

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;

    }

}

【问题讨论】:

  • 转换为列表并使用retainAll:docs.oracle.com/javase/7/docs/api/java/util/…
  • @canillas can not use any collections
  • 所以使用 2 个循环,迭代和比较。使用谷歌...有很多这样的例子。
  • @canillas 我正在浏览 Google,但没有任何包含 3 个数组的示例
  • @СимеонПецанов 您可以使用Integer.MIN_VALUE 之类的默认值来区分正确的,即答案部分显示的Integer.MIN_VALUE 生成的默认值

标签: java arrays elements items


【解决方案1】:

如果有人想知道@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

【讨论】:

  • 你是对的,忘记了Arrays.sort() 已就位,已更正。
  • common 数组的最大 大小为min(sorted1.length, sorted2.length)。您分配的内存过多。
  • @PiotrWilkin 我认为 OP 说不要使用 Collection。虽然我不确定Arrays是否可以称为Collection
  • Arrays 只是一个实用类,其中包含处理数组的函数。根据定义,集合是从 Collection 继承的东西。
  • (请不要让我从头写一个合并排序函数)
【解决方案2】:

您可以使用 arr[i] = Integer.MIN_VALUE; 为新数组 arr 中的元素使用 MIN 或 MAX 默认虚拟值。这样,您将能够区分真实值和虚拟值。如下:

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;

    }

输出

[4, 5, -2147483648, -2147483648, -2147483648]

编辑

结论 当您迭代 arr 时,除 -2147483648 之外的所有值都是通用的。

编辑 2

打印下面评论中提到的通用值:

public static void main(String[] args) {
checkArrayItems obj = new checkArrayItems();
int[] arr = obj.checkArr(array1, array2);
        System.out.println("Common values are : ");
        for (int x : arr) {
            if (x != Integer.MIN_VALUE) {
                System.out.print(x+"\t");
            }
        }
}

建议:遵循类的命名约定,即将checkArrayItems 改为CheckArrayItems

【讨论】:

  • 你是如何遍历这个数组的。无论我如何尝试,我总是返回一个 hashCode?抱歉这个愚蠢的问题
  • 你的意思是从checkArr 方法返回后的arr?这些值是 Integer.MIN_VALUE 返回的值
  • 代码很完美,只是我不知道如何打印出你上面给出的结果。抱歉:)
  • @СимеонПецанов 立即查看
【解决方案3】:

我懒得打代码,但这里是算法。 1.对两个数组进行排序 2. 遍历数组比较项目并增加索引。

希望这会有所帮助。

【讨论】:

  • 因为它比二次比较(每个与每个)更快(n log n)。
  • @Eugene:不,这与二分搜索无关。如果使用朴素算法,则必须将数组 A 的每个元素与数组 B 的每个元素进行比较,从而使算法成为二次的。如果先对数组进行排序,则可以改为保留两个数组的当前索引(从 A 开始)并使用“追逐”算法,在该算法中,使用较小的当前元素增加数组的索引,直到获得一个元素大于或等于另一个数组上的当前元素(如果元素相等,则将其添加到“公共元素”数组中)。
  • @Eugene:后一种算法受排序复杂度(O(max(n,m)log max(n,m)))支配,因为“追逐”算法本身是线性的两个数组大小的总和。
【解决方案4】:

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

int index = 0;

这将保存 arr 数组的当前位置。那么:

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

而且,由于您使用 arr1.lenght 初始化 arr,因此您的数组将在 not colsion 的末尾用 0 填充。

【讨论】:

  • 为此他可以使用Integer.MIN_VALUE 之类的默认值,即在第一个循环中不常见
猜你喜欢
  • 2021-11-08
  • 2011-11-06
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 2018-01-09
  • 2020-06-04
  • 2017-05-11
相关资源
最近更新 更多