Two options:
- Create a list of values you wish to remove, adding to that list within the loop, then call
originalList.removeAll(valuesToRemove)
at the end - Use the
remove()
method on the iterator itself. Note that this means you can’t use the enhanced for loop.
As an example of the second option, removing any strings with a length greater than 5 from a list:
List<String> list = new ArrayList<String>(); ... for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) { String value = iterator.next(); if (value.length() > 5) { iterator.remove(); } }
Related Posts:
- How does the Java ‘for each’ loop work?
- How does the Java ‘for each’ loop work?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- When to use LinkedList over ArrayList in Java?
- Convert list to array in Java [duplicate]
- Initialization of an ArrayList in one line
- How to sort an ArrayList?
- What’s the C++ version of Java’s ArrayList
- Create ArrayList from array
- Best way to convert an ArrayList to a string
- “Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0″ with ArrayList?
- Best way to convert an ArrayList to a string
- Print ArrayList
- Print ArrayList
- How to make a deep copy of Java ArrayList
- Array ArrayList python equivalent
- Java ArrayList copy
- how to iterate in List
- > in java and set their values as we do in a normal int a[i][j] matrix type [duplicate]
- How to convert an ArrayList containing Integers to primitive int array?
- How do I apply the for-each loop to every character in a String?
- Print ArrayList
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- HashSet vs. ArrayList
- Java ArrayList replace at specific index
- How can I create an Array of ArrayLists?
- How to create an 2D ArrayList in java?
- What does the colon (:) operator do?
- Error: cannot find symbol ArrayList
- java howto ArrayList push, pop, shift, and unshift
- How to get the last value of an ArrayList
- How to add an object to an ArrayList in Java
- Add String Array to ArrayList
- Incompatible types List of List and ArrayList of ArrayList
- Size has private access in ArrayList
- How to write a test class to test my code?
- How to convert a String into an ArrayList?
- Java ArrayList of Doubles
- How to create a Multidimensional ArrayList in Java?
- convert string to arraylist
in java - What is the difference between ArrayList.clear() and ArrayList.removeAll()?
- for each loop in groovy
- ShoppingCart.Java Program Assignment
- How Do I Implement an Insertion Sort Method for A Generic ArrayList?
- toring and Retrieving ArrayList values from hashmap
- return an ArrayList method
- How can I convert IPV6 address to IPV4 address?
- Spring Boot – Unable to resolve Whitelabel Error Page
- When do you use Java’s @Override annotation and why?
- Java Error “Exception in thread “main” java.util.InputMismatchException” On an Array program
- getting error HTTP Status 405 – HTTP method GET is not supported by this URL but not used `get` ever?
- What is a classpath and how do I set it?
- Editor does not contain a main type in Eclipse [duplicate]
- Invalid initial heap size -Xms4096M
- How do I use a PriorityQueue?
- What does Java option -Xmx stand for?
- Global variables in Java
- Does the Project Lombok @Data annotation create a constructor of any kind?
- Javac is not found
- How Exactly Does @param Work – Java
- Return string Input with parse.string
- Android ListView headers
- What does -XX:MaxPermSize do?
- What does .class mean in Java?
- What does the following Oracle error mean: invalid column index
- how to fix java.lang.arrayindexoutofboundsexception: 0?
- Convert the string into stringBuilder in java
- “NoClassDefFoundError: Could not initialize class” error
- Recursion vs. Iteration (Fibonacci sequence)
- What is difference between Errors and Exceptions?
- What is the difference between Integer and int in Java?
- Static Error: This class does not have a static void main method accepting String[]
- “uses unchecked or unsafe operations”
- possible lossy conversion from long to int?
- How to sort an array of objects in Java?
- Android – SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
- keytool error Keystore was tampered with, or password was incorrect
- How to override toString() properly in Java?
- Returning null in a method whose signature says return int?
- keytool error Keystore was tampered with, or password was incorrect
- How to clear console in Java – Eclipse
- Java TreeMap Comparator
- What is the use of System.in.read()?
- Invalid escape sequence (valid ones are \b \t \n \f \r \” \’ \\ )
- JDK was not found on the computer for NetBeans 6.5
- In Java, how to assign the variable number=Integer.parseInt(args[0]) a value if no argument is passed?
- How are “mvn clean package” and “mvn clean install” different?
- Why are two empty ArrayLists with different generic types equal?
- Android – Start service on boot
- JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object
- Sending POST data in Android
- When is “java.io.IOException:Connection reset by peer” thrown?
- what is update-alternatives command in linux and what is the use of it?
- java: use StringBuilder to insert at the beginning
- Multiple delimiters in Scanner class of Java
- Eclipse Problems View not showing Errors anymore
- Test if element is present using Selenium WebDriver?
- Increase heap size in Java
- android.content.res.Resources$NotFoundException: String resource ID #0x0
- what does x– or x++ do here?
- How to specify filepath in java?