2015-09-18 36 views
1

我有一個ArrayList,我需要從中刪除元素。從ArrayList中刪除一個元素的字符串包含特定字符串的項目

我得到的輸出是這樣的:

ArrayList: [2062253<:>2, 2062254<:>242.0, 2062252<:>100] 

我是能夠刪除2062254<:>242.0。我可以用.remove("2062254<:>242.0")刪除這個項目,但問題是這個字符串總是改變。字符串不變的唯一部分是54<:>

有沒有辦法通過使用類似的東西從一個arraylist中刪除一個元素:.contains("54<:>")

也許我可以做的,如果檢查表如下:

if (calList.contains("54<:>")) { 
    //How can I get the index ID here? Remove this index from the arraylist 
} 

回答

3

你必須去通過列表,檢查每一個元素:

Iterator<String> it = calList.iterator(); 
while (it.hasNext()) { 
    if (it.next().contains("54<:>")) { 
    it.remove(); 
    // Add break; here if you want to remove just the first match. 
    } 
} 
+0

這是完美的,非常感謝 – mhorgan

相關問題