Removing an object from an array

I have an arraylist with names, phone numbers and locations.

I would like to remove some elements from this array, if possible

Is there a way to remove an item as soon as I found it as I tried below?

public void delete(String nameToDelete) { for (Entry entry : Directory.entries) { if (entry.name.equalsIgnoreCase(nameToDelete)) { //remove(entry); } } } 

thanks

+2
source share
3 answers

Cause?

The iterators returned by ArrayList have fail-fast .

The iterators returned by this class by the iterator and listIterator are fail-fas t: if the list is structurally altered at any time after creating the iterator, in any way other than through its own methods to remove or add an iterator, the iterator will throw a ConcurrentModificationException . Thus, in the face of simultaneous modification, the iterator is fast and clean, and does not risk arbitrary, non-deterministic behavior at an undetermined time in the future.

Where does this iterator come from? Until I use it?

For the extended loop for collections, Iterator used, so you cannot call the remove method during iteration.

So your loop is the same as below

 for (Iterator<Entry> i = c.iterator(); i.hasNext(); ){ 

What is the solution?

You can call iterator.remove(); and change the iterator-based loop explicitly, not implicitly.

  String inputWord = "john"; ArrayList<String> wordlist = new ArrayList<String>(); wordlist.add("rambo"); wordlist.add("john"); for (ListIterator<String> iterator = wordlist.listIterator(); iterator .hasNext();) { String z = iterator.next(); if (z.equals(inputWord)) { iterator.remove(); } } System.out.println(wordlist.size()); 

Now where can I find out more?

+2
source

You must use Iterator to remove the entry from your list. If you try to change your List when iterating through the extended for loop, it may throw a ConcurrentModificationException , although this does not always happen, but there is no guarantee.

Here you can use Iterator for your list: -

 Iterator<Entry> iterator = Directory.entries.iterator(); while (iterator.hasNext()) { Entry entry = iterator.next(); if (entry.name.equalsIgnoreCase(nameToDelete)) { iterator.remove(); } } 

Even in Iterator you must remove the element using the iterator.remove method, and not using the list.remove method, because it will not matter.

You can also use ListIterator , which gives you even more operations and methods for iterating over the list. See the documentation for more details.

To use ListIterator , you can create it like this: -

 ListIterator<Entry> listIterator = Directory.entries.listIterator(); 

And the rest of the code works the same way.

+1
source

What would you do is use if entry.getKey().equals(nameToDelete) to check if you need it, and then use the remove() method to remove this entry.

However, you cannot do this inside a for() loop, iterating over the same collection that you are modifying. You will get a ConcurrentModificationException .

See "Avoiding ConcurrentModificationException When Using Iterator" .

0
source

Source: https://habr.com/ru/post/1498923/


All Articles