mhorgan
mhorgan

Reputation: 886

Removing an item from an ArrayList where an element's string contains a certain string

I have an ArrayList that I need to delete an element from.

I get an output like this:

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

I was to be able to delete 2062254<:>242.0. I could just delete the item with a .remove("2062254<:>242.0") but the thing is that the string always changes. The only part of the string that doesn't change is the 54<:>.

Is there a way to remove an element from an arraylist by using something like: .contains("54<:>")?

Maybe I could do an if check list this:

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

Upvotes: 1

Views: 67

Answers (1)

Andy Turner
Andy Turner

Reputation: 140319

You have to go through the list and check each element:

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

Upvotes: 3

Related Questions