【问题标题】:Comparing two arrays and then making another array with common elements (and no duplicates)比较两个数组,然后用公共元素制作另一个数组(并且没有重复)
【发布时间】:2018-05-28 21:46:37
【问题描述】:

您好,我是 Java 新手,我正在做这个实验任务,我们比较两个数组的元素以获得共同的元素。我被困在如何摆脱重复项上。

我当前的代码给了我输出 [3, 0, 5, 6, 5, 0, 9, 0],而想要的 common 输出是 [3, 5, 6, 9, 0, 0, 0, 0]。

另外,由于我没有那么有经验,请不要发布解决问题的专业方法或“有经验”的回答我的问题,因为这对我没有任何帮助:D。

谢谢!

 public static void main (String[] args) {
    int[] a1 = {3, 8, 5, 6, 5, 8, 9, 2};
    int[] a2 = {5, 15, 4, 6, 7, 3, 9, 11, 9, 3, 12, 13, 14, 9, 5, 3, 13};
    int[] common = new int[a1.length];
    System.out.println("Exercise 3: ");
    findCommon(a1,a2,common);
 }



public static void findCommon(int[] a1, int[]a2, int[] common) {
    int num = 0;

      for (int i = 0; i < common.length; i++)
      {
          for (int j = 0; j < a2.length; j++)
          {
              if (a1[i] == a2[j]) // loops through every index of j, while keeping i at one index
                  num = a1[i];
                  for (int k = 0; k < common.length; k++) // makes sure there are no duplicates in common
                 {
                     if (num != common[k]) 
                      common[i] = num;
                 }
          }
      }

      for (int elements : common)
          System.out.print(elements + " ");
  }

【问题讨论】:

标签: java arrays for-loop foreach


【解决方案1】:

您应该考虑使用Sets 来做这种事情,但由于这是一个练习,我已经在代码中提供了一个带有一些 cmets 的解决方案。

基本上,您应该将问题分解为多个部分,每个部分都有自己的方法。这样一来,您就可以更轻松地解决问题。

  1. arrayIntersect(int[], int[])

    此方法的工作是从两个数组创建一个数组。结果数组必须具有两个数组中都存在的唯一元素。

你可以这样做。 1 带有辅助方法(如下所述)。

  1. inArray(int, int[])

    如果数组包含给定元素,此方法返回 true,否则返回 false。

例子

public static void main (String[] args) {
    int[] a1 = {3, 8, 5, 6, 5, 8, 9, 2};
    int[] a2 = {5, 15, 4, 6, 7, 3, 9, 11, 9, 3, 12, 13, 14, 9, 5, 3, 13};

    int[] a3 = arrayIntersect(a1, a2);

    for (int a : a3) {
        System.out.println(a);
    }
}

private static int[] arrayIntersect(int[] a1, int[] a2) {
    int[] intersect = new int[Math.min(a1.length, a2.length)];

    int curIndex = 0;
    for (int x : a1) {
        if (inArray(x, a2) && !inArray(x, intersect)) {
            intersect[curIndex] = x;
            curIndex++;
        }
    }

    // resize intersect array to not include unused indexes
    int[] tmp = intersect;
    intersect = new int[curIndex];

    for (int i = 0; i < intersect.length; i++) {
        intersect[i] = tmp[i];
    }

    return intersect;
}

private static boolean inArray(int element, int[] array) {
    boolean result = false;

    for (int a : array) {
        if (element == a) {
            result = true;
            break;
        }
    }

    return result;
}

【讨论】:

  • 为您提供了正确的解决方案而被标记,但在练习之外,我通常更愿意看到使用 System.arraycopy() 而不是手动复制数组。
【解决方案2】:

您非常接近正确的 anwser,但是 for 循环 for (int k = 0; k &lt; common.length; k++) 正在为 a2 的每个元素执行。因此,当 a1 存在一个值但 a2 不存在时,您将 num 的旧值放在公共数组中。如果您查看打印的元素,您会看到每次在 a1 中存在一个元素时,该元素都会重复。 代码的结果是

3 3 5 6 5 5 9 9

您输入了正确的标识,但忘记了大括号。如果将大括号放在if (a1[i] == a2[j]) 处,结果如下:

3 0 5 6 5 0 9 0

但是为什么会有这些 0 呢?因为当您在 java 中创建一个 int 数组时,所有元素的值都以 0 开头。并且您将公共元素放置在 a1 数组中存在此元素的相同位置。您可以通过使用无效数字填充 int 数组并忽略它来纠正此问题。在这段代码中,我假设 -1 是一个无效值。

public static void findCommon(int[] a1, int[] a2, int[] common) {
    int num = 0;
    for (int i = 0; i < common.length; i++) {
        common[i] = -1;
    }

    for (int i = 0; i < common.length; i++) {
        for (int j = 0; j < a2.length; j++) {
            if (a1[i] == a2[j]) { // loops through every index of j, while keeping i at one index
                num = a1[i];
                for (int k = 0; k < common.length; k++) // makes sure there are
                // no duplicates in common
                {
                    if (num != common[k])
                        common[i] = num;
                }
            }
        }
    }

    for (int elements : common) {
        if (elements != -1)
            System.out.print(elements + " ");
    }
}

如果你看到,在if (elements != -1) 我没有放大括号,但它有效。如果你不放大括号,它只会执行下一个命令。

【讨论】:

  • 我明白你在做什么,感谢你没有让代码过于复杂。但是,(我忘了提到这一点),我的代码所需的输出是 3 5 6 9 0 0 0 0,它应该打印整个公共数组,但只为未更改的索引打印零。你知道如何修改代码来做到这一点吗?
  • 您总是将公共元素放在它第一次出现在 a1 数组中的位置。您可以创建另一个 int 保存在您将放置该元素的位置。因此,总是在公共数组中放置一些值之后,将该 int 加一。开始时,aux 值为 0,将第一个元素放在位置 common[aux] 后,执行 aux++。由于我无法对您的问题发表评论,因此建议您将当前代码的结果放入问题中。通常,它有助于找到答案。
  • 谢谢,我想通了!我使用了一个索引变量(在 for 循环之外声明),我用它来增加 common[]。我还使用了一个布尔值,它将在 j 循环中重置为 false,并且将是 k 循环中的唯一语句。之前,我认为 common[] 没有得到完全检查,所以我将 if 语句移到 k-loop 之外
猜你喜欢
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 2021-11-08
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
相关资源
最近更新 更多