【问题标题】:Compare two array and return an array to show which elements are equal or not比较两个数组并返回一个数组以显示哪些元素相等或不相等
【发布时间】:2017-04-21 16:04:52
【问题描述】:

我有两个数组。我想比较两个数组并返回一个数组,它是第一个元素等于或不等于另一个数组的数组。 例如:

int[] arr1 = {1,2,3,4,5}; 
int[] arr2 = {1,3};

我想返回一个数组或哈希图来显示哪些元素相等或不相等。

boolean[] arr = {true, false, true, false, false};

OR

HashMap<Integer, String> map = new HashMap<Integer, String>();           
map.put(1, "true");  
map.put(2, "false");  
map.put(3, "true");  
map.put(4, "false");  
map.put(5, "false");

Output: {1=true, 2=false, 3=true, 4=false, 5=false}

这是我的代码。这只显示相等的值。

for (Int arr1 : arr1) {
        for (Int arr2 : arr2) {
            if (arr2.equals(arr1)) {
                booleanValue = true;
                System.err.println(arr2 + ", " + arr1 + ", " + booleanValue);
            }
        }
    }

请帮我解释一下逻辑。谢谢你

【问题讨论】:

  • 你能分享你创建布尔数组或 HashMap 的尝试吗?

标签: java arrays loops


【解决方案1】:

不要使用 foreach 循环,而是使用 fori 循环。对于 arr1 的每个元素,检查 arr2 的每个元素是否匹配,然后将该索引的 arr 设置为 true 并中断循环。

int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = {1, 3};
boolean[] arr = new boolean[arr1.length];

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

System.out.println(Arrays.toString(arr));

【讨论】:

    【解决方案2】:

    在第二个循环中比较两个矩阵是否相等时,您必须将标志设置为 true 并中断第二个循环,因此第一个循环将比较下一个元素,例如:

    int[] arr1 = {1, 2, 3, 4, 5};
    int[] arr2 = {1, 3};
    
    HashMap<Integer, Boolean> map = new HashMap<>();
    
    for (int i : arr1) {
        for (int j : arr2) {
            if (j == i) {
                map.put(i, true);
                break;
            } else {
                map.put(i, false);
            }
        }
    }
    
    System.out.println(Arrays.asList(map));
    

    输出:

    [{1=true, 2=false, 3=true, 4=false, 5=false}]
    

    【讨论】:

      【解决方案3】:

      我会做类似的事情

      public static void main(String[] args) {
          HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
          int[] arr1 = {1, 2, 3, 4, 5};
          int[] arr2 = {1, 3};
          for (int a : arr1) {
              map.put(a, isElementInArray(a, arr2));
          }
      
          System.out.println(Arrays.asList(map));
      }
      
      public static boolean isElementInArray(int element, int[] array) {
          for (int a : array) {
              if (a == element) {
                  return true;
              }
          }
          return false;
      }
      

      输出:

      [{1=true, 2=false, 3=true, 4=false, 5=false}]
      

      【讨论】:

        【解决方案4】:

        有一个很好的方法来处理流:

        int[] arr1 = {1, 2, 3, 4, 5};
        int[] arr2 = {1, 3};
        
        Set<Integer> set2 = new HashSet<>();
        for (int a : arr2) {
            set2.add(a);
        }
        
        Map<Integer, Boolean> map = Arrays.stream(arr1).boxed()
            .collect(Collectors.toMap(Function.identity(), set2::contains));
        
        System.out.println(map); // {1=true, 2=false, 3=true, 4=false, 5=false}
        

        首先我们从arr2创建一个HashSet,然后我们流式传输arr1的元素,我们将它们装箱(这样我们可以在下一步中收集它们),最后我们收集Integer的元素流到map中:key是实际的Integer元素,value是一个布尔值,表示这个元素是否属于set2

        编辑:如果您希望映射的值是字符串而不是布尔值,您可以稍微更改值映射器函数:

        Map<Integer, String> map2 = Arrays.stream(arr1).boxed()
            .collect(Collectors.toMap(e -> e, e -> String.valueOf(set2.contains(e))));
        
        System.out.println(map2); // {1=true, 2=false, 3=true, 4=false, 5=false}
        

        【讨论】:

          猜你喜欢
          • 2012-02-09
          • 2012-05-21
          • 2019-03-23
          • 2020-02-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多