2015-01-15 5 views
1

문자열의 ArrayList에 문자열 배열의 각 50 개의 문자열이 하나씩 있고 ArrayList의 모든 동일한 문자열에 대해 무언가를 수행하는지 확인하는 방법은 무엇입니까?StringList의 ArrayList에 50 개의 다른 문자열이 하나씩 포함되어 있는지 확인하십시오.

+0

당신이 한 일을 우리에게 보여줄 수 있습니까? – Unlockedluca

+0

double for loop로 할 수 있을까요? –

+0

난 그냥 아이디어가 필요해 –

답변

3

배열의 모든 문자열이 ArrayList에도 있는지 확인하는 데이 함수를 사용할 수 있습니다. 일치가 발견 될 때마다 doSomething()과 같은 추가 논리를 추가하려면 코드를 쉽게 적용 할 수 있어야합니다.

ArrayList myList; // let's assume its initialized and filled with Strings 
String[] strArray; // let's assume its initialized and filled with Strings 

//this function returns true if all Strings in the array are also in your arraylist 

public boolean containsAll(myList, strArray){ 
    //iterate your String array 
    for(int i = 0; i < strArray.length; i++){ 
    if(!myList.contains(strArray[i])){ 
     //String is not in arraylist, no need to check the rest of the Strings 
     return false; 
    } 
    } 
return true; 
} 
+0

고마워, 그게 내가 무엇을 필요로하는지 –

2

왜 LINQ를 사용하지 않습니까? 이, 당신이 원하는 경우에, 그래서는 원본 목록에서 중복되는 항목을 알고 싶어, 새로운 List<string>의 모든 중복을 반환하면 결과 순서에 고유 적용하거나 위의 솔루션을 사용할 수

List<String> duplicates = YourList.GroupBy(x => x) 
         .Where(g => g.Count() > 1) 
         .Select(g => g.Key) 
         .ToList(); 

+1

LINQ 대안 솔루션 upvote – naomik

관련 문제