Method to delete specific elements in java collection class arraylist loop
During project development, we may often need to dynamically delete some elements in the ArrayList.
A wrong way:
<pre name="code" class="java">for(int i = 0 , len= list.size();i<len;++i){ if(list.get(i)==XXX){ list.remove(i); } }
The above method will throw the following exception:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at ListDemo.main(ListDemo.java:20)
Because you deleted the element, but did not change the iteration subscript, so when the iteration reaches the last one An exception will be thrown.
The above program can be improved as follows:
for(int i = 0 , len= list.size();i<len;++i){ if(list.get(i)==XXX){ list.remove(i); --len;//减少一个 } }
The above code is correct.
Let’s introduce another solution below:
The List interface implements the Iterator interface internally, providing developers with an iterator() to get an iterator object of the current list object.
Iterator<String> sListIterator = list.iterator(); while(sListIterator.hasNext()){ String e = sListIterator.next(); if(e.equals("3")){ sListIterator.remove(); } }
The above is also correct, and the second option is recommended.
The implementation principles of the two solutions are quite different. The second one is just encapsulated by jdk.
Looking at the ArrayList source code, you will find that many methods are internally implemented based on the iterator interface, so it is recommended to use the second solution.
The above is the entire method of deleting specific elements in the java collection class arraylist loop brought to you by the editor. I hope you will support the PHP Chinese website~
More related methods of deleting specific elements in the java collection class arraylist loop Please pay attention to the PHP Chinese website for articles!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

This article explores integrating functional programming into Java using lambda expressions, Streams API, method references, and Optional. It highlights benefits like improved code readability and maintainability through conciseness and immutability

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

This article explains Java's NIO API for non-blocking I/O, using Selectors and Channels to handle multiple connections efficiently with a single thread. It details the process, benefits (scalability, performance), and potential pitfalls (complexity,

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

This article details Java's socket API for network communication, covering client-server setup, data handling, and crucial considerations like resource management, error handling, and security. It also explores performance optimization techniques, i
