猿问

JAVA - 比较两个数组并创建一个仅包含第一个数组的唯一值的新数组

我必须按照以下标准解决一个练习:


比较两个数组:


int[] a1 = {1, 3, 7, 8, 2, 7, 9, 11};

int[] a2 = {3, 8, 7, 5, 13, 5, 12};

array int[]使用第一个数组中的唯一值创建一个新的。结果应如下所示:int[] result = {1,2,9,11};


注意:我不允许使用ArrayList或Arrays上课来解决此任务。


我正在使用以下代码,但填充循环的逻辑不正确,因为它引发了越界异常。


public static int[] removeDups(int[] a1, int[] a2) {

    //count the number of duplicate values found in the first array

    int dups = 0;

    for (int i = 0; i < a1.length; i++) {


        for (int j = 0; j < a2.length; j++) {

            if (a1[i] == a2[j]) {

                dups++;

            }

        }

    }

    //to find the size of the new array subtract the counter from the length of the first array

    int size = a1.length - dups;

    //create the size of the new array

    int[] result = new int[size];


    //populate the new array with the unique values

    for (int i = 0; i < a1.length; i++) {

        int count = 0;

        for (int j = 0; j < a2.length; j++) {

            if (a1[i] != a2[j]) {

                count++;

                if (count < 2) {

                    result[i] = a1[i];

                }

            }

        }

    }


    return result;

}

我也很想知道如何用一个循环来解决这个问题(学习目的)。


拉丁的传说
浏览 172回答 4
4回答

慕码人8056858

我提供以下解决方案。迭代第一个数组,找出它的最小值和最大值。创建长度为max-min+1 的临时数组(您可以使用max + 1作为长度,但当您有值时,例如从 100k 开始,它可能会遵循开销)。迭代第一个数组并标记临时数组中的现有值。迭代第二个数组并取消标记临时数组中的现有值。将临时数组中的所有标记值放入结果数组。代码:public static int[] getUnique(int[] one, int[] two) {&nbsp; &nbsp; int min = Integer.MAX_VALUE;&nbsp; &nbsp; int max = Integer.MIN_VALUE;&nbsp; &nbsp; for (int i = 0; i < one.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; min = one[i] < min ? one[i] : min;&nbsp; &nbsp; &nbsp; &nbsp; max = one[i] > max ? one[i] : max;&nbsp; &nbsp; }&nbsp; &nbsp; int totalUnique = 0;&nbsp; &nbsp; boolean[] tmp = new boolean[max - min + 1];&nbsp; &nbsp; for (int i = 0; i < one.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int offs = one[i] - min;&nbsp; &nbsp; &nbsp; &nbsp; totalUnique += tmp[offs] ? 0 : 1;&nbsp; &nbsp; &nbsp; &nbsp; tmp[offs] = true;&nbsp; &nbsp; }&nbsp; &nbsp; for (int i = 0; i < two.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; int offs = two[i] - min;&nbsp; &nbsp; &nbsp; &nbsp; if (offs < 0 || offs >= tmp.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; if (tmp[offs])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; totalUnique--;&nbsp; &nbsp; &nbsp; &nbsp; tmp[offs] = false;&nbsp; &nbsp; }&nbsp; &nbsp; int[] res = new int[totalUnique];&nbsp; &nbsp; for (int i = 0, j = 0; i < tmp.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; if (tmp[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res[j++] = i + min;&nbsp; &nbsp; return res;}

慕莱坞森

出于学习目的,我们不会添加新工具。让我们按照你之前的思路,修正第二部分:// populate the new array with the unique valuesfor (int i = 0; i < a1.length; i++) {&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; for (int j = 0; j < a2.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (a1[i] != a2[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count < 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[i] = a1[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}对此://populate the new array with the unique valuesint position = 0;for (int i = 0; i < a1.length; i++) {&nbsp; &nbsp; boolean unique = true;&nbsp; &nbsp; for (int j = 0; j < a2.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (a1[i] == a2[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unique = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (unique == true) {&nbsp; &nbsp; &nbsp; &nbsp; result[position] = a1[i];&nbsp; &nbsp; &nbsp; &nbsp; position++;&nbsp; &nbsp; }}我假设您实施的“计数”是为了防止误报添加到您的结果数组中(这会结束)。当一个人确定一个数组是否包含 dup 时,他不做“计数”,他只是通过沿着列表向下比较第一个数字和第二个数组,然后如果他看到一个 dup (a1[i] == a2[j]),他会说“哦,它不是唯一的”(unique = false)然后停止循环(break)。然后他会将数字添加到第二个数组中 (result[i] = a1[i])。所以要尽可能地结合这两个循环:// Create a temp Array to keep the data for the loopint[] temp = new int[a1.length];int position = 0;for (int i = 0; i < a1.length; i++) {&nbsp; &nbsp; boolean unique = true;&nbsp; &nbsp; for (int j = 0; j < a2.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (a1[i] == a2[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unique = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (unique == true) {&nbsp; &nbsp; &nbsp; &nbsp; temp[position] = a1[i];&nbsp; &nbsp; &nbsp; &nbsp; position++;&nbsp; &nbsp; }}// This part merely copies the temp array of the previous size into the proper sized smaller arrayint[] result = new int[position];for (int k = 0; k < result.length; k++) {&nbsp; &nbsp; result[k] = temp[k];}

Cats萌萌

让你的代码工作如果您更正第二个循环,您的代码工作正常。看看我所做的修改://populate the new array with the unique valuesint counter = 0;for (int i = 0; i < a1.length; i++) {&nbsp; &nbsp; for (int j = 0; j < a2.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (a1[i] == a2[j]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result[counter] = a1[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我会这样做的方式现在,这是我如何创建这样的方法,而无需多次检查重复项。往下看:public static int[] removeDups(int[] a1, int[] a2) {&nbsp; &nbsp; int[] result = null;&nbsp; &nbsp; int size = 0;&nbsp; &nbsp; OUTERMOST: for(int e1: a1) {&nbsp; &nbsp; &nbsp; for(int e2: a2) {&nbsp; &nbsp; &nbsp; &nbsp; if(e1 == e2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue OUTERMOST;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; int[] temp = new int[++size];&nbsp; &nbsp; &nbsp; if(result != null) {&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i < result.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp[i] = result[i];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; temp[temp.length - 1] = e1;&nbsp; &nbsp; &nbsp; result = temp;&nbsp; &nbsp; }&nbsp; &nbsp; return result;}它不是创建result具有固定大小的数组,而是在每次发现新的重复项时创建一个具有适当大小的新数组。请注意,如果等于,则返回null。a1a2

Helenr

您可以使用另一种方法来查看元素是否包含在列表中:public static boolean contains(int element, int array[]) {&nbsp; &nbsp; for (int iterator : array) {&nbsp; &nbsp; &nbsp; &nbsp; if (element == iterator) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return false;}您的 main 方法将迭代每个元素并检查它是否包含在第二个元素中:int[] uniqueElements = new int[a1.length];int index = 0;for (int it : a1) {&nbsp; &nbsp;if (!contains(it, a2)) {&nbsp; &nbsp; &nbsp; &nbsp;uniqueElements[index] = it;&nbsp; &nbsp; &nbsp; &nbsp;index++;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答