简体   繁体   English

如何将两个 arrays 中的常用词保存到第三个数组中?

[英]How do I save common words from two arrays into a third array?

I am trying to save all common words from arr1 and arr2 and then store to arr3.我正在尝试保存 arr1 和 arr2 中的所有常用词,然后存储到 arr3。

String[] arr1 = {"pEace", "happiness", "Gives", "So", "aaa"};
String[] arr2 = {"pEace", "HappiNess", "joy", "give", "AAA", "enjoy", "learN"};

int k = 0;
String[] arr3 = new String[0];
for (int i = 0; i < arr1.length; i++) {
    for (int j = 0; j < arr2.length; j++) {
        if (arr1[i].equalsIgnoreCase(arr2[j])) {
            System.out.println(arr1[i]);
            arr3[k] = arr1[i];
            k++;
        }
    }
}
System.out.println(Arrays.toString(arr3));

The init size of arr3 is 0 arr3 的初始大小为 0

String[] arr3 = new String[0];字符串[] arr3 = 新字符串[0];

You should init with size is total length of arr1 and arr2 or use ArrayList .您应该初始化大小为 arr1 和 arr2 的总长度或使用ArrayList On that example are 12在那个例子中是 12

You can convert these arrays to collections and then use the retainAll method.您可以将这些arrays转换为collections然后使用retainAll方法。 It works differently in different collections.它在不同的 collections 中工作方式不同。

Try it online! 在线尝试!

// Convert arrays to collections
String[] arr1 = {"pEace", "happiness", "Gives", "So", "aaa", "learn"};
String[] arr2 = {"pEace", "HappiNess", "joy", "give", "AAA", "learN"};

Collection<String> coll1 = Arrays.asList(arr1);
Collection<String> coll2 = Arrays.asList(arr2);
// ArrayList
ArrayList<String> list1 = new ArrayList<>();
ArrayList<String> list2 = new ArrayList<>();

list1.addAll(coll1);
list2.addAll(coll2);

list2.retainAll(list1);

System.out.println(list2); // [pEace]
// TreeSet
TreeSet<String> set1 = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
TreeSet<String> set2 = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);

set1.addAll(coll1);
set2.addAll(coll2);

set2.retainAll(set1);

System.out.println(set2); // [AAA, HappiNess, learN, pEace]

See also: Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet另请参阅: TreeSet 是否可能等于 HashSet 但不是 HashSet 等于 TreeSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 for 和 if 语句将两个 arrays 中的匹配数字添加到空的第三个数组中? - How can I add the matching numbers from two arrays to an empty third array using for and if statements? 如何从给定的两个字符串数组形成第三个java数组 - How to form a Third java Array from given Two String Arrays 使用来自两个数组的信息来计算第三个数组的内容 - Using info from two arrays to calculate contents of third array Java映射两个数组中的数据并将其插入第三个数组 - Java mapping data from two arrays and insert into third array 如何打印第一个数组中的单词,第二个数组中的第二个和第三个数组中的第三个的单词组合? - How to print a combination of words one from first array the second from second array and third from third array? 如何在Java中将数组中的数字排序为两个不同的数组? - How do I sort numbers from an array into two different arrays in java? 如何将两个数组之间的交集作为新数组? - How do I get the intersection between two arrays as a new array? 从两个字符串中得到两个常用词 - get two common words from two string 如何比较两个数组列表并提取第三个数组列表中两个元素都不相同的元素 - How to compare two array list and extract the element in third array list that are not common in both 如何从两个数组存储公共值 - how to store common values from two array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM