比较两个字符串数组的内容,并删除两者中出现的字符串

时间:2016-03-08 01:04:48

标签: java arrays nested-loops string-comparison

这是一个项目程序,我暂时无法解决这个问题,我希望有人可以指出我错过的代码问题。此代码的目标是比较两组数组(A和B)并创建第三个数组,该数组仅包含仅在B中出现的条目。 我的方法是初始化第三个数组并用“”填充每个条目。从那里比较两个给定的数组,如果A中的条目没有出现在A中,那么该条目被添加到第三个数组中。但是,当我测试我写的内容时,数组B完全按原样复制到第三个数组中,不会删除任何条目。 这是我的代码。 从我的微弱的人脑中查看代码,我觉得这应该工作,但事实并非如此。 如果字符串A包含“计算机” 和字符串B包含“计算机”,“是”,“你” 字符串C应该是“是”,“你” 但是运行这段代码,String C就是“计算机”,“是”,“你”

   public static String [] findPatternInSentence( String [] A, String [] B) {
        if (A== null){
            return null;
        }
        String[] C= new String[A.length+1];
            for (int p = 0; p <  A.length+1; p++){
            C[p]= "";
                    }

                for( int i = 0; i< B.length; i++){
                int k = Eliza.inList(A[0], B);
                  if(k > -1){
                    int j = 0;
                if(A[j].equals(B[i]) && j < A.length-1){
                        j++;
                }
                else {

                    C[j] = C[j] + " " + B[i];
                }
            }
            if (k == -1)
            {
                return null;
            }  
        }
        return C;
    }

1 个答案:

答案 0 :(得分:0)

例如,假设您的数组包含

String[] arr1 = {"a","b","c"};
String[] arr2 = {"a","b","c","d"};

利用套装..

// add everything from arr2
Set set = new TreeSet(Arrays.asList(arr2)); // ["a","b","c","d"]

// remove everything that showed up in arr1
set.removeAll(Arrays.asList(arr1)); // ["d"]

如果您不能使用套装并希望采用基于阵列的方法(利用地图)并关注频率,而不仅仅是出现您在帖子中未提及的元素..

 String[] arr1 = {"a", "b", "c", "a", "a", "b", "c", "a"};
 String[] arr2 = {"a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d"};

 int totalElems = 0;

 Map<String, Integer> freq = new HashMap<String, Integer>();

 // add stuff from arr2
 for(String _b: arr2) {
     totalElems++;
     if(!freq.containsKey(_b)) 
         freq.put(_b, 0);
    freq.put(_b, freq.get(_b)+1); 
 }

 // add stuff from arr1, removing stuff that were in arr2
 for(String _a: arr1) {
     if(freq.containsKey(_a) && freq.get(_a) > 0) {
         totalElems--;
         freq.put(_a, freq.get(_a)-1); 
     }
 }

 String[] c = new String[totalElems];
 int ptr = 0;
 for(String key: freq.keySet()) {
     int count = freq.get(key);
     while(count != 0) {
         c[ptr++] = key;
         count--;
     }
 }
 System.out.println(Arrays.toString(c)); // [b, c, d, d, d]

如果你只是关心那些没有出现在A ..

中的元素B.
 String[] arr1 = {"a", "b", "c", "a", "a", "b", "c", "a"};
 String[] arr2 = {"a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d"};

 int totalElems = 0;

 Map<String, Boolean> freq = new HashMap<String, Boolean>();

 // add stuff from arr2
 for(String _b: arr2) {
     if(!freq.containsKey(_b)) {
         totalElems++;
         freq.put(_b, true);
     } 
 }

 // add stuff from arr1, removing stuff that were in arr2
 for(String _a: arr1) {
     if(freq.containsKey(_a) && freq.get(_a)) {
         totalElems--;
         freq.put(_a, false); 
     }
 }

 String[] c = new String[totalElems];
 int ptr = 0;
 for(String key: freq.keySet()) {
     if(freq.get(key))
         c[ptr++] = key;
 }
 System.out.println(Arrays.toString(c));  // [d]

根据我所写的内容,它应涵盖大多数情况。如果您需要进一步的帮助,请告诉我。如果您无法使用Maps,我看到的只有array A的{​​{1}}的所有元素都会在给定提供的信息的情况下查看array B的每个元素。

我也只是添加它。这个实现应该有效(我还没有测试过)

String[] c = new String[b.length];
int cPtr = 0;
for(int i = 0 ; i < b.length ; i++) {
    boolean found = false;
    for(int j = 0 ; j < a.length ; j++) {
        if(a[j].equals(b[i])) {
            found = true;
            break;
        }
    }
    if(!found)
        c[cPtr++] = b[i];
}