2014-06-23 17 views
0

請參閱我的下面的代碼,請讓我知道更正。 我寫了我的預期結果..請幫助我。我想分開類似的值在數組中在java

try 
{ 
    List<String> assocIds= new ArrayList<String>(); 

    List<String> actionids= new ArrayList<String>(); 
    assocIds.add("1"); 
    assocIds.add("2"); 
    assocIds.add("3"); 
    assocIds.add("5"); 

    actionids.add("2"); 
    actionids.add("3"); 
    actionids.add("7"); 
    actionids.add("4"); 
    actionids.add("6"); 


    List<String> matchFromFirstArray= new ArrayList<String>(); 
    List<String> notMatchFromFisrtArray= new ArrayList<String>(); 
    List<String> notMatchFromSecondArray= new ArrayList<String>(); 

    for(int j=0;j<assocIds.size();j++) 
    { 
     for(int i=0;i<actionids.size();i++) 
     { 
      if(assocIds.get(j).equalsIgnoreCase(actionids.get(i))) 
      { 
       matchFromFirstArray.add(assocIds.get(j)); 

      }else 
      { 
       notMatchFromFisrtArray.add(assocIds.get(j)); 
       notMatchFromSecondArray.add(actionids.get(i)); 
      } 
     } 
    } 

    System.out.println("match from first array : "+matchFromFirstArray.toString()); 
    System.out.println("not match from fisrt array : "+notMatchFromFisrtArray.toString()); 
    System.out.println("unmatch form second array : "+notMatchFromSecondArray.toString()); 


}catch (Exception e) { 
    System.out.println("Error :"+e); 
} 
} 

My Expected Result:

從第一陣列匹配:[2,3]

不從最前一頁陣列匹配:[1,5]

UNMATCH形式第二陣列:[7,4 1,6]

My OUTPUT:

從第一匹配rray:[2,3]

與fisrt陣列不匹配:[1,1,1,1,1,2,2,2,3,3,3,5,5,5, 5,5]

不匹配形式的第二陣列:[2,3,7,4,6​​,3,7,4,6​​,2,7,4,6​​,2,3,7,4,6​​]

+0

您是否嘗試過自己做任何調試? – awksp

+0

http://stackoverflow.com/questions/12919231/finding-the-intersection-of-two-arrays ?? – TheLostMind

回答

1
List<String> matchFromFirstArray= new ArrayList<String>(); 
List<String> notMatchFromFisrtArray= new ArrayList<String>(); 
List<String> notMatchFromSecondArray= new ArrayList<String>(); 

matchFromFirstArray.addAll(assocIds); 
matchFromFirstArray.retainAll(actionids); // retains all matching elements 


notMatchFromFisrtArray.addAll(assocIds); 
notMatchFromFisrtArray.removeAll(matchFromFirstArray); // retains all not matching elements from first array 



notMatchFromSecondArray.addAll(actionids); 
notMatchFromSecondArray.removeAll(matchFromFirstArray); // retains all not matching elements from second array 
+0

謝謝.. :) – Addy