2017-02-26 1 views
-4
for (int i = 0; i < arrayA.length; i++) { 

     for (int y = 0; y < arrayB.length; y++) { 
      if (arrayA[i] == arrayB[y]) { 
       cnt++; 
      } 
     } 
    } 

    if (cnt == arrayB.length) { 
     // B is subset of A 
    } 

答えて

1

あなたはcontainsAll方法のために:例えば、

List<String> list1 = Arrays.asList(a); 
List<String> list2 = Arrays.asList(b); 
list1.containsAll(list2); 

HereのJavadocをlistsに配列を変換し、これを確認するためにcontainsAllメソッドを使用することができます。

更新

は、ここでは、intアレイの場合に動作する方法は次のとおりです。

int[] a = new int[10]; 
int[] b = new int[10]; 
List<Integer> list1 = Arrays.stream(a).boxed().collect(Collectors.toList()); 
List<Integer> list2 = Arrays.stream(b).boxed().collect(Collectors.toList()); 
list1.containsAll(list2); 
+0

プリミティブ型 'int型[]'のような場合には、それが配列である場合はどう? 'Arrays.asList'はこのケースを正しく処理できず、リストを埋めるために他の方法が必要になることに言及する価値があります。 – Pshemo

+0

配列の型に基づいて、コレクションの種類を適宜変更することができます。原始的であれば、 'stream'を使ってボックス化することさえできます。 –

+0

それは本当です。 IMOそれは答えに入れる価値がある。 – Pshemo

関連する問題