[問題] 想請問Hashtable<Key,Values>的問題

看板java作者 (莫急莫慌莫害怕)時間9年前 (2015/12/10 17:41), 9年前編輯推噓1(102)
留言3則, 2人參與, 最新討論串1/2 (看更多)
發出求救信號~ 我有一個Hashtable<Key,Values> 裡面的Values一直更新 想要判斷Values連續10次相同就remove掉這個device的array 我有寫出大概意思的code不過不知道怎麼下手,因我的mRssi會一直更新 public ArrayList<BluetoothDevice> mLeDevices = new ArrayList<BluetoothDevice>(); public ArrayList<byte[]> mRecords = new ArrayList<byte[]>(); public Hashtable<String, Integer> mRssi = new Hashtable<>(); public void addDevice(BluetoothDevice device, int rssi, byte[] scanRecord) { if (!mLeDevices.contains(device)) { mLeDevices.add(device); mRecords.add(scanRecord); } mRssi.put(device.getAddress(), rssi); /** 以下為意識code */ ArrayList<Integer> rssiArrayList = new ArrayList<Integer>(mRssi.values()); int lastNum = rssiArrayList.get(); int count = 0; /** 從頭開始loop */ for (int i = 0; i < rssiArrayList.size(); i++) { /** 如果當前值等於最後值 */ if(rssiArrayList[i]==lastNum){ count++; if(count>=10){ /** 如果連續10次rssi相同就移除 */ mLeDevices.remove(); } } /** 如果不相等的話 */ else { lastNum=rssiArrayList[i]; count = 1; } } Log.e("mRssi", String.valueOf(mRssi)); } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.161.11.105 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1449740510.A.347.html

12/11 00:32, , 1F
確定一下,你是希望同一個Device相同rssi 出現10次才會
12/11 00:32, 1F

12/11 00:32, , 2F
移除嗎?
12/11 00:32, 2F
→你好,就是我的藍芽在掃瞄, 然後要顯示在RecyclerView上, 當我掃瞄到的時候添加進去, 可是當我把藍芽裝置的電池拔開的時候, 沒偵測到了, 需要自動remove掉那個item, 希望有simple code可以提供~ 已經用盡我自己的方式還是不行真是不好意思呢

12/11 05:39, , 3F
做一個新的 class,就兩個 member: lastNum 和 count
12/11 05:39, 3F
有比較具體一點的code嗎,不好意思 ※ 編輯: freedom74569 (61.230.134.93), 12/11/2015 07:39:00
文章代碼(AID): #1MQKZUD7 (java)
文章代碼(AID): #1MQKZUD7 (java)