【问题标题】:How to return an array that contains values, which appear in two arrays? And no duplicates如何返回包含值的数组,这些值出现在两个数组中?并且没有重复
【发布时间】:2019-04-18 15:35:13
【问题描述】:

我想传递两个名称数组,以便它返回一个数组,其中包含出现在一个或两个数组中的名称。返回的数组应该没有重复。

例如,调用MergeNames.uniqueNames(new String[]{'Ava', 'Emma', 'Olivia'}, new String[]{'Olivia', 'Sophia', 'Emma'}) 应该返回一个包含任意顺序的 Ava、Emma、Olivia 和 Sophia 的数组。

需要基本实现uniqueNames方法。抱歉,我是 Java 编程的新手,并试图通过尝试编码挑战来成为一名开发人员。

public class MergeNames {

    public static String[] uniqueNames(String[] names1, String[] names2) {
        throw new UnsupportedOperationException("Waiting to be implemented.");      
    }

    public static void main(String[] args) {
        String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
        String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};
        System.out.println(String.join(", ", MergeNames.uniqueNames(names1, names2))); // should print Ava, Emma, Olivia, Sophia
    }
}

******我的解决方案欢迎任何反馈******

import java.util.*;

public class MergeNames {

public static void main(String[] args) {

    String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
    String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};

    Set<String> mySet1 = new HashSet<String>(Arrays.asList(names1));
    Set<String> mySet2 = new HashSet<String>(Arrays.asList(names2));

    Set<String> union = new HashSet<String>(mySet1);
    union.addAll(mySet2);
    System.out.println("Union of the two Sets with no duplicate names : " + union);

}
}

我不确定为什么需要 uniqueNames 函数?

【问题讨论】:

  • 那么继续:试试吧!试一试,我们很乐意帮助您解决具体问题。
  • 这闻起来像家庭作业,但如果我真的必须在 Java 中这样做,数组可能不是我会使用的集合类型。
  • 是的。每当您需要对数据集合进行操作时,如果 Java 集合 api 中已有 .retainAll() 方法,则数组不会是首选
  • @RShome 尝试找出可用于保存唯一值的数据结构。
  • 你基本上是想创建一个集合。 docs.oracle.com/javase/7/docs/api/java/util/Set.html

标签: java arrays


【解决方案1】:

在 cmets 中您提到您必须以不同的方法执行此操作并将 联合数组作为参数返回给main()。那么通过这个声明我认为他们的意思是你必须将return联合数组返回main()“不作为参数”

你可以这样:

import java.util.*;

public class MergeNames 
{
    public static void main(String[] args) 
    {
        String[] names1 = new String[] {"Ava", "Emma", "Olivia"};
        String[] names2 = new String[] {"Olivia", "Sophia", "Emma"};
        String[] Names = mergeNames(names1, names2);
        for(String n: Names)
            System.out.print(" "+ n);
    }

    public static String[] mergeNames(String[] n1, String[] n2)
    {
        Set<String> mySet1 = new HashSet<String>(Arrays.asList(n1));
        Set<String> mySet2 = new HashSet<String>(Arrays.asList(n2));
        Set<String> union = new HashSet<String>(mySet1);
        union.addAll(mySet2);
        return  union.toArray(new String[union.size()]);
    }
}

【讨论】:

  • 谢谢。抱歉,我仍在尝试掌握一些编码概念。不明白为什么我觉得它这么难。
  • 很高兴为您提供帮助......快乐的编码:)
【解决方案2】:

正如@junvar 建议的那样,看看java.util.Set

  • 创建new java.util.TreeSet&lt;String&gt; myset
  • 循环遍历每个数组并为每个名称值调用myset.add()。 Set 将为您删除重复项
  • 要将您的集合转换回数组,请参阅Converting Set<String> to String array

祝你好运!

【讨论】:

    【解决方案3】:
    import java.util.ArrayList;
    
    public class tes1 {
    
        public static String[] uniqueNames(String[] names1, String[] names2) {
    
            List<String> list = new ArrayList<String>();
            for (String name : names1) {
                list.add(name);
            }
            for (String name : names2) {
                if (!list.contains(name)) {
                    list.add(name);
                }
            }
            String[] a = list.toArray(new String[list.size()]);
            return a;
    
        }
    
        public static void main(String[] args) {
            String[] names1 = new String[] { "Ava", "Emma", "Olivia" };
            String[] names2 = new String[] { "Olivia", "denene", "haloil", "Sophia", "Emma" };
    
            System.out.println(String.join(", ", tes1.uniqueNames(names1, names2)));
        }
    }
    

    【讨论】:

      【解决方案4】:
      public static String[] uniqueNames(String[] names1, String[] names2) {
      
        if(names1 == null || names2 == null){
          return null;
        }
      
        HashSet<String> set = new HashSet<>();
        for(String name : names1){
          set.add(name);
        }
        for(String name : names2){
          set.add(name);
        }
        System.out.println(set);
        return set.toArray(new String[set.size()]);
      }
      

      【讨论】:

        【解决方案5】:
         public static String[] uniqueNames(String[] names1, String[] names2) {
                List<String> resArray = new ArrayList<>(Arrays.asList(names1));
                resArray.addAll(Arrays.asList(names2));
                return new HashSet<>(resArray).toArray(new String[0]);
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-20
          • 2013-07-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多