【问题标题】:Compare contents of two string arrays, and remove strings which occur in both比较两个字符串数组的内容,并删除两者中出现的字符串
【发布时间】:2016-03-08 01:04:47
【问题描述】:

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

   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;
    }

【问题讨论】:

  • 你检查我的答案了吗?如果它没有帮助,我会删除它

标签: java arrays nested-loops string-comparison


【解决方案1】:

例如,假设您的数组包含

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 B 的每个元素遍历array A 的所有元素。

我也将添加它.. 这个实现应该可以工作(我还没有测试过)

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];
}

【讨论】:

  • 可以,但是我不允许使用集合(这是一个项目,我可能应该在原帖中这么说,对不起)。目标是使用循环集和开关来填充数组 C。
  • @UVB-76 没问题。最好将答案标记为正确,以便将来的用户可以更快地找到答案。
猜你喜欢
  • 2012-12-03
  • 2010-11-17
  • 1970-01-01
  • 2020-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
相关资源
最近更新 更多