2014-02-24 74 views
1

我有一個問題,我想不通。 我有一個電話號碼列表,其表示爲:用戶有應用程序 我也有我的手機中所有聯繫人的樹形圖 我想比較所有擁有該應用程序的用戶到我的聯繫人樹形圖,以便如果用戶有應用程序 - 我可以將它們添加爲朋友,但如果他們不這樣,我可以發送文本邀請。這是我的,但它不工作:迭代器的循環邏輯

Iterator<Entry<String, String>> it = contacts.entrySet().iterator(); 
      while (it.hasNext()) { 
       Map.Entry<String, String> pairs = (Map.Entry<String, String>)it.next(); 
       for(int i = 0; i < users.size(); i++) { 
        String userNumber = Helper.formatNumber(users.get(i).getString("phone")); 
        if(pairs.getValue().equals(userNumber)) { //they have the app 
         contact_data.add(new Contact(pairs.getKey().toString(), pairs.getValue().toString(), true)); 
        } else { //they don't have the app 
         contact_data.add(new Contact(pairs.getKey().toString(), pairs.getValue().toString(), false)); 
        } 
       } 
       it.remove(); // avoids a ConcurrentModificationException 
      } 
+1

詳細說明「它不工作」。你收到什麼錯誤信息?目前的結果是什麼? – Tristan

回答

0

每次您檢查用戶是否匹配您正在查看的聯繫人,您添加該用戶。看起來你只想爲每個聯繫人添加一個用戶。

一種方法是在for循環之前添加一個布爾型「found」並將其初始化爲false。一旦找到擁有該應用的用戶,請將此布爾值設置爲true。在for循環後面加上標記爲「//他們沒有應用程序」的else分支,並且只有在(!found)時才運行它。這將確保您只爲每個聯繫人添加一個聯繫人數據。