2017-03-08 6 views
-7

2つの配列リストがあります。私はその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'のようなリテラルは '** ** String'リテラルではありません – QBrute

+4

あなたのコードはコンパイルされません。整数を文字列( '" 10 "' vs '10')に変更すると、うまくいきます。 – tnw

+0

申し訳ありませんが、コード全体が含まれていません。これらの文字列はサーブレットから渡されており、DAOに渡しています。コードを更新します。 – Gee

答えて

0

私は正解で質問を更新しました。

0

配列に値が重複していない場合や、出現していない要素だけに興味がある場合は、Setを使用できます。

二組に配列を変換する:

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

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

one.removeAll(secondSet);

two.removeAll(firstSet);

両方のセットが空の場合、両方のセットは等しくなり、そうでなければ各セットに固有の要素があります。

関連する問題