2015-09-18 33 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

相关问题