gpt4 book ai didi

java - 从 ArrayList 中删除一个项目,其中元素的字符串包含某个字符串

转载 作者:行者123 更新时间:2023-11-29 04:59:09 24 4
gpt4 key购买 nike

我有一个 ArrayList,我需要从中删除一个元素。

我得到这样的输出:

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

我是能够删除2062254<:>242.0 .我可以删除带有 .remove("2062254<:>242.0") 的项目但问题是字符串总是在变化。字符串中唯一不变的部分是 54<:> .

有没有一种方法可以使用类似以下的方法从数组列表中删除元素:.contains("54<:>")

也许我可以做一个 if 检查列表:

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

最佳答案

您必须遍历列表并检查每个元素:

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.
}
}

关于java - 从 ArrayList 中删除一个项目,其中元素的字符串包含某个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32655833/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com