简体   繁体   English

你如何从Java中减去另一个chars数组?

[英]How do you subtract one array of chars from another in Java?

Let's say I have an array, arrayA = ["a", "b", "c", "d", "e", "f"], and another array, arrayB = ["a", "d", "e"]. 假设我有一个数组,arrayA = [“a”,“b”,“c”,“d”,“e”,“f”]和另一个数组,arrayB = [“a”,“d”, “E”]。

I want to subtract arrayB from arrayA to yield the result = ["b", "c", "f"] 我想从arrayA中减去arrayB以得到结果= [“b”,“c”,“f”]

This is my setup for each array: 这是我为每个阵列设置的:

char[] arrayA = new char[7];
for(char c = 'a'; c <= 'f'; ++c) {
    arrayA[c - 'a'] = c;
}
char[] arrayB = new char[]{'a','d','e'};

(Please excuse any improper use of symbols and syntax, I'm a Ruby noob trying to learn Java simply from the Oracle tutorials. Thanks!) edit: a word and quotes (请原谅任何不正确的符号和语法使用,我是一个Ruby noob尝试简单地从Oracle教程学习Java。谢谢!)编辑:一个单词和引号

The short answer is to convert your arrays to "sets", and then use set operations on them. 简短的回答是将数组转换为“集合”,然后对它们使用set操作。 I'm looking for the proper code for that right now, but you can start by checking out this post: Classical set operations for java.util.Collection 我现在正在寻找适当的代码,但你可以先看一下这篇文章: java.util.Collection的经典集合操作

Edit: Luke657 brings up a good point. 编辑:Luke657提出了一个很好的观点。 primitive arrays are weird. 原始数组很奇怪。 So below is the updated code: 以下是更新的代码:

Assuming you start with a char array (it would of course be better to start with a set, but oh well): 假设你从一个char数组开始(当然最好先从一个集合开始,但是很好):

char[] arrayA = new char[] {'a', 'b', 'c', 'd', 'e', 'f'};
char[] arrayB = new char[] {'a', 'd', 'e'};
Character[] objarrayA = ArrayUtils.toObject(arrayA);
Character[] objarrayB = ArrayUtils.toObject(arrayB);
Set<T> setA = new HashSet(Arrays.asList(objarrayA));
Set<T> setB = new HashSet(Arrays.asList(objarrayB));

setA.removeAll(setB);

Then, to get it back to a char array: 然后,将其返回到char数组:

Character[] result;
result = setA.toArray(result);
char[] cresult = ArrayUtils.toPrimitive(result);

I believe this will do what you need. 我相信这会做你需要的。 The Arrays.asList() operation is O(1), so is efficient and not computationally expensive, so don't worry about that extra conversion. Arrays.asList()操作是O(1),因此效率高且计算成本不高,所以不要担心额外的转换。

Convert them to List and call the removeAll method: 将它们转换为List并调用removeAll方法:

Character[] array1 = ArrayUtils.toObject(arrayA);
    Character[] array2 = ArrayUtils.toObject(arrayB);       
    List<Character> list1 = new ArrayList(Arrays.asList(array1));
    List<Character> list2 = new ArrayList(Arrays.asList(array2));
    list1.removeAll(list2);`

I suggest you construct a Set from arrayA and then call removeAll on it using the second array. 我建议你从arrayA构造一个Set ,然后使用第二个数组调用removeAll

If the two arrays are sorted as shown in your question you can solve the problem with a single iteration over the arrays. 如果两个数组按照您的问题中所示进行排序,则可以通过对阵列的单次迭​​代来解决问题。

import java.util.Collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Repeated {

public static void main(String[] args) {
//        Collection listOne = new ArrayList(Arrays.asList("a", "b", "c", "d", "e", "f"));
//        Collection listTwo = new ArrayList(Arrays.asList("a", "d", "e"));
 //
//        listOne.retainAll( listTwo );
//        System.out.println( listOne );

    String[] s1 = {"a", "b", "c", "d", "e", "f"};
    String[] s2 = {"a", "d", "e"};
    List<String> s1List = new ArrayList(Arrays.asList(s1));
    for (String s : s2) {
        if (s1List.contains(s)) {
            s1List.remove(s);
        } else {
            s1List.add(s);
        }
         System.out.println("intersect on " + s1List);
    }
}
}

Arrays.asList doesn't work with primitive types like char so you have to iterate through both arrays, change them to the wrapper class Character and add them to sets. Arrays.asList不适用于像char这样的基本类型,因此您必须遍历两个数组,将它们更改为包装类Character并将它们添加到集合中。 Then, you can use removeAll method. 然后,您可以使用removeAll方法。

Set<Character> setA = new HashSet<>();
Set<Character> setB = new HashSet<>();
for(int i = 0; i < arrayA.length; i++){
    setA.add(new Character(arrayA[i]));
}
for(int i = 0; i < arrayB.length; i++){
    setA.add(new Character(arrayB[i]));
}
setA.removeAll(setB);
arrayA = new char[setA.size()];
int i = 0;
for(Character c : setA){
    arrayA[i++] = c.charValue();
}

Convert your arrays to lists (For example ArrayList) using Arrays.asList() . 使用Arrays.asList()将数组转换为列表(例如ArrayList Arrays.asList() Generic sets do not take primitive types (so asList won't work on your arrays as they are now) so you can use the object Character instead like this: 通用集不采用原始类型(因此asList不会像现在一样对你的数组起作用)所以你可以像这样使用对象Character:

Character a[] = {'f', 'x', 'l', 'b', 'y'};
Character b[] = {'x', 'b'};
ArrayList<Character> list1 = new ArrayList<Character>(Arrays.asList(a));
ArrayList<Character> list2 = new ArrayList<Character>(Arrays.asList(b));
list1.removeAll(list2);

Read about generic types here in case you're unfamiliar with them: http://docs.oracle.com/javase/tutorial/java/generics/types.html 如果您不熟悉它们,请阅读有关通用类型的信息: http//docs.oracle.com/javase/tutorial/java/generics/types.html

In case you do need arrays you can use the toArray() function of ArrayList to recreate an array. 如果您确实需要数组,可以使用ArrayList的toArray()函数重新创建数组。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM