2017-04-08 5 views
0

私のプログラムは正常に動作していますが、結果は間違っています。私はHashMap<Integer, ArrayList<Integer>>ArrayList<Integer>を使用し、私のプログラムは新しいHashMap<integer, ArrayList<Integer>>を生成します。プログラムはHashMapの各値とArrayListの値を比較し、同様の値を追加して結果を新しいHashMapに格納する必要があります。あなたの理解を深めるための写真を追加しました。hashmapとarraylistの値が類似しています

algorithm

例:

ArrayList<Integer> : 
    [1, 0, 1, 0, 0] 
Hashmap<Integer,ArrayList<integer>> : 
    1, [1, 0, 1, 1, 0] 
    2, [0, 1, 1, 0, 0] 
    3, [0, 1, 1, 1, 0] 

結果:

HashMap 
    1, 4 
    2, 3 
    3, 2 

コード:

System.out.println("HASHMAP SIMILIRATE RESULT:"); 
HashMap<Integer, Integer> sim = new HashMap<Integer, Integer>(); 

int count; 
int k = 1; 
for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) { 
    count = 0; 

    //you can move this part to another method to avoid deep nested code 
    for (Integer mapValue : e.getValue()) { 
     if (mapValue.equals(listOperateur.get(k))) { 
      count = count + 1;   
     } 
    } 

    sim.put(e.getKey(), count); 
    k++; 
} 
+1

あなたのコードを投稿してください。 – stinepike

+0

@StinePike 投稿したことは申し訳ございません – fatoma

+0

今日は早くOPを手伝ってみましたが、コードはあまりにも厄介で、彼の問題はちょっと意味がありません。 –

答えて

0
int k = 0; // initiate k with 0 
for (Map.Entry<Integer, ArrayList<Integer>> e : hmm.entrySet()) { 
    count = 0; 

    //you can move this part to another method to avoid deep nested code 
    for (Integer mapValue : e.getValue()) { 
     if (mapValue.equals(listOperateur.get(k++))) { 
      count = count + 1;   
     } 
    } 

    sim.put(e.getKey(), count); 
    k=0; 
} 
+0

あなたは歓迎です:)。あなたはそれが役に立つ見つけた場合、あなたは答えをupvoteと喜び – stinepike

+0

を閉じそして、最も重要なのは、私はあなたのせいではいはい、私はよく理解 はどうもありがとうございましたコード – fatoma

+0

でOKこの – stinepike

関連する問題