In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to solve the trampling pit of Java List's remove () method". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the trampling pit of Java List's remove () method".
Java's List typically uses the list.remove (o) / remove (I) method when deleting elements. When in use, it is easy to touch traps and get unexpected results. Summarize the past experience, record it and share it with you.
Initialize the List first, as follows:
Package com.cicc.am.test; import java.util.ArrayList;import java.util.List; public class ListTest {public static void main (String [] args) {List list=new ArrayList (); list.add (1); list.add (2); list.add (3); list.add (3); list.add (4); System.out.println (list);}}
The output result is [1,2,3,3,4]
1. Ordinary for loop traverses List to delete the specified element-error! For (int iposit I = size) throw new NoSuchElementException (); Object [] elementData = ArrayList.this.elementData; if (I > = elementData.length) throw new ConcurrentModificationException (); cursor = I + 1; return (E) elementData [lastRet = I] } public void remove () {if (lastRet < 0) throw new IllegalStateException (); checkForComodification (); try {ArrayList.this.remove (lastRet); cursor = lastRet; lastRet =-1; expectedModCount = modCount } catch (IndexOutOfBoundsException ex) {throw new ConcurrentModificationException ();}} final void checkForComodification () {if (modCount! = expectedModCount) throw new ConcurrentModificationException ();}}
Through the code, we find that Itr is a private inner class defined in ArrayList. The checkForComodification method is called in both next and remove methods. The purpose of this method is to determine whether modCount! = expectedModCount is equal, and throw a ConcurrentModificationException exception if it is not equal. Every time the remove method is executed normally, expectedModCount = modCount will be assigned to ensure that the two values are equal, then the problem is basically clear, in the foreach loop
Execute list.remove (item);, which modifies the modCount value of the list object, but not the expectedModCount value of the iterator of the list object, so a ConcurrentModificationException exception is thrown.
5. Delete the List element iteratively-correct!
All collection object types in java implement the Iterator interface, and the traversal can be iterated:
Iterator it=list.iterator (); while (it.hasNext ()) {if (it.next () = = 3) {it.remove ();}} System.out.println (list)
Output result: [1, 2, 4]
The Iterator.remove () method deletes the current iteration object while retaining the index of the original element. Therefore, it is the safest way to delete elements iteratively. It is recommended that you use the List process.
Use this method when you need to delete an element in.
6. Iterate through, delete the element with the list.remove (I) method-error! Iterator it=list.iterator (); while (it.hasNext ()) {Integer value=it.next (); if (value==3) {list.remove (value);}} System.out.println (list)
Throw an exception: java.util.ConcurrentModificationException, the principle is the same as the above method 4.
7. When List deletes elements, pay attention to the difference between the Integer type and the int type.
For the list of the above Integer, delete element 2 directly. The code is as follows:
List.remove (2); System.out.println (list)
Output result: [1,2,3,4]
As you can see, when List deletes an element, when a number is passed in, it is deleted by index by default. If you need to delete the Integer object, call the remove (object) method and pass in the Integer type, as shown below:
List.remove (new Integer (2)); System.out.println (list)
Output result: [1,3,3,4]
Thank you for your reading, the above is the content of "how to solve the trampling pit of Java List remove () method". After the study of this article, I believe you have a deeper understanding of how to solve the problem of trampling pit of Java List remove () method, and the specific use situation still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.