2017-03-08 8 views
-7

2 개의 배열 목록이 있습니다. 저는이 둘 사이의 고유 한 가치를 되돌리고 싶습니다. 어떻게 이뤄지나요?루프리스트를 통해 2 개의 배열 목록을 비교하고 일치 항목을 찾습니다.

String[] s1 = {10, 1}; 
String[] s2 = {10, 1, 13}; 

//loop through and compare element of s1 to s2  
//switch varialbe used to indicate whether a match was found 
boolean matchFound = false; 

//outer loop for all the elements in s2 
for (int i = 0; i < s2.lenght; i++) { 
    //inner loop for all the elements in s1 
    for (int i = 0; i < s1.lenght; i++) { 
    matchFound = true; 
    System.out.println("This " + s2[i] + "was found"); 
    } 
} 
if(matchFound == false) { 
    System.out.println("This " + s2[i] + "was not found"); 
} 
//set matchFound bool back to false 
matchFound = false; 
} 
+2

'10'과'1'과 같은 리터럴은 ** 문자열 리터럴이 아닙니다 ** – QBrute

+4

코드가 컴파일되지 않습니다. 정수를 문자열 (' "10"'vs'10')로 변경하면 잘 작동합니다. – tnw

+0

전체 코드가 포함되지 않아서 죄송합니다. 이 문자열은 내 서블릿에서 전달되며 DAO로 전달됩니다. 코드를 업데이트합니다. – Gee

답변

0

정답으로 질문을 업데이트했습니다.

0

배열에 중복 값이 ​​없거나 출현에없는 요소에만 관심이있는 경우 집합을 사용할 수 있습니다.

두 가지 세트로 배열을 변환 :

Set<T> one = new HashSet<T>(Arrays.asList(s1));

Set<T> two = = new HashSet<T>(Arrays.asList(s2));

one.removeAll(secondSet);

two.removeAll(firstSet);

두 세트가 모두 비어 있으면 두 세트가 동일하고 그렇지 않으면 각 세트에 고유 한 요소가 있습니다.