簡體   English   中英

比較兩個字符串數組的內容,並刪除兩個字符串數組中都出現的字符串

[英]Compare contents of two string arrays, and remove strings which occur in both

這是一個項目程序,一段時間以來我一直無法解決此問題,希望有人可以指出我所缺少的代碼問題。 此代碼的目的是比較兩組數組(A和B),並創建僅由僅在B中出現的條目組成的第三個數組。我的方法是初始化第三個數組,並將每個條目填充為“”。 從那里比較兩個給定的數組,如果B中沒有在A中出現一個條目,則將該條目添加到該第三個數組中。 但是,當我測試自己編寫的內容時,數組B會完全照原樣復制到第三個數組中,而不會刪除任何條目。 這是我的代碼。 通過用微弱的人腦仔細閱讀代碼,我覺得這應該可行,但事實並非如此。 如果字符串A包含“計算機”,而字符串B包含“計算機”,“是”,“您”,則字符串C應該是“ are”,“ you”,但仍在運行此代碼,則字符串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;
    }

例如,說您的數組包含

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的每個元素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];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM