【问题标题】:Return sum of Index for matching Array values返回匹配数组值的索引总和
【发布时间】:2020-04-13 23:02:06
【问题描述】:

我正在尝试用 Java 编写一个函数,该函数采用两个数组并将数组 1 中的索引值相加,其中值与数组 2 匹配,例如

  • Array1 = {15, 6, 99, 12, 35}
  • Array2 = {1, 12, 7, 99, 35}
  • 匹配“Array1[index]”值 = 2 (99)、3 (12)、4 (35)
  • 所以,返回 9 (2+3+4)

我建议使用以下方法:

public int sumIndex(int[] array1, int[] array2) {
    int total = 0;
    for (int i=0; i < array1.length; i++) {
        for (int j = 0; j < array2.length; j++) {
            if (array1[i] == array2[j]) {
                total = total + i;
            }
        }
    }
}

但如果 Array2 中有不匹配的值,我也想返回 -1。因此,在上述情况下,1 和 7 不在 Array1 中,因此总数会减少 -2(每个缺失值都为 -1)。

如果我添加一个“else”语句

else {
    total = total - 1;
}

或'else if'语句

else if (array1[i] != array2[j]) {
    total = total - 1;
}

每次在 i 和 j 循环中存在 array1[index] 与 array2[index] 不匹配的值时,我都会从总数中删除 -1。我试图只为 array2 中与 array1 中的值不匹配的值返回 -1。

我将如何编写以便仅在 array2 中的值不是 array1 的元素时才返回 total-1?

谢谢

【问题讨论】:

  • 您好,感谢您的回复。我在发布包含代码时遇到问题,我一直收到错误消息,要求使用 Ctrl+K 格式化代码。使用 Ctrl+K 无法解决问题并阻止我发布,因此我将代码添加为图像。
  • 我已经重新输入了代码部分并且修改已被接受。如果您还有其他问题或 cmets,请告诉我。谢谢
  • Array 1 或 Array 2 中是否有重复值?例如 Array 1 中是否可以有两个元素的值为 15 ?与{15, 6, 99, 12, 15} 一样,使用stream API 的解决方案是否可以接受?
  • 可以允许重复值。不允许进口。不必使用循环,但这就是我试图解决它的方法。
  • 我建议您edit您的问题并添加一个具有重复值的数组的示例,以及您对此类数组的预期结果。

标签: java arrays


【解决方案1】:

看看你的例子,我假设数组有独特的元素。在这种情况下,您需要做以下事情:

  1. 找到匹配项后立即中断内部循环。这将使您的代码高效。
  2. 更改循环的顺序并重复相同的逻辑。
  3. 在内循环终止时,检查内循环是否提前终止(因为break)。如果没有,则未找到匹配项,因此需要将 total 的值减少 1。
public class Main {
    public static void main(String[] args) {
        System.out.println(sumIndex(new int[] { 15, 6, 99, 12, 35 }, new int[] { 1, 12, 7, 99, 35, 100, 1000 }));
    }

    static int sumIndex(int[] array1, int[] array2) {
        int total = 0, i, j;
        // Nested loops to calculate sum of indices having equal values
        for (i = 0; i < array1.length; i++) {
            for (j = 0; j < array2.length; j++) {
                if (array1[i] == array2[j]) {
                    total += i;
                    break;
                }
            }
        }

        // Nested loops to decrease 'total' for the non-matching values of array2
        for (j = 0; j < array2.length; j++) {
            for (i = 0; i < array1.length; i++) {
                if (array1[i] == array2[j]) {
                    break;
                }
            }

            // If a match is not found, 'i' will reach a value equal to array1.length
            if (i == array1.length) {
                total--;
            }
        }
        return total;
    }
}

输出:

5

【讨论】:

  • 第二个 if 语句将为 Array2 中未找到的所有 Array1 值返回 -1。不适用于在 Array1 中找不到的任何 Array2 值。
  • @SidheGuido - 是否将订单交换到循环并不重要。无论如何,重要的是if (array1[i] == array2[j])。您是否期望从上面给出的代码中得到与7 不同的结果?如果有任何进一步的疑问/问题,请随时发表评论。
  • 这只是检查数组 1 中的值。如果将更多 int 值添加到数组 2,结果仍然是 7,即如果我要添加 {100, 1000} 到数组 2,结果仍然是 7 . 如果我将 {100, 1000} 添加到数组 1,结果为 5。如果值在数组 2 中但不在数组 1 中,我希望返回 -1,因此如果添加了 {100, 1000},我将需要总计 = 5到数组 2。
  • 啊...现在我更清楚地了解您的要求...我已经更新了我的答案。如果有任何进一步的疑问/问题,请随时发表评论。
  • @SidheGuido - 明白了。我已经更新了答案。如果有任何进一步的疑问/问题,请随时发表评论。
【解决方案2】:

只需创建一个标志found,在每个循环中重置它,如果array1[i] 值在array2[j] 中找到,则将其设置为true,如果它为false,则将total 减1。

public int sumIndex(int[] array1, int[] array2) {
    total = 0;
    for (int i=0; i < array1.length; i++) {
        bool found = false;
        for (int j = 0; j < array2.length; j++) {
            if (array1[i] == array2[j]) {
                total = total + i;
                found = true;
            }
        }
        if(!found)
            total--;
    }
}

【讨论】:

  • 对于来自 Array1 的不匹配值,这似乎返回 -1,即如果您有两个值未包含在 Array 1 中,一个值未包含在 Array 2 中,您将得到 -2 而不是 - 1.
【解决方案3】:

您应该使用列表而不是数组。如果你使用列表,你可以使用:

List<Integer> arrayList1 = Arrays.asList(15, 6, 99, 12, 35);
List<Integer> arrayList2 = Arrays.asList(1, 12, 7, 99, 35);
int indexSum = 0;
for(Integer integer : arrayList2){
        indexSum = indexSum + arrayList1.indexOf(integer);
}

当arrayList2 的元素integer 不在ArrayList1 中时,indexOf 方法返回-1。如果存在,该方法将返回索引。因此,如果它不存在,则将 -1 添加到 indexSum。如果存在,则添加索引。

【讨论】:

  • 您可以在for 循环中使用短路运算符,如:indexSum += arrayList1.indexOf(integer);
猜你喜欢
  • 2017-12-15
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多