Skip to content

How to remove an element and the element after that in an ArrayList if the value of the element is less than a specific integer?

I have this program where I need to remove the value from an ArrayList and the value after that if the value is less than 10.

Here is an example:

Original ArrayList:

[20, 40, 15, 4, 25, 50, 45]

Modified ArrayList: (if the value is less than 10)

[20, 40, 15, 50, 45] – (Removed 4 and 25 because 4 is less than 10)

I have made a program that remove the value that is less than 10, but I can’t figure out how to remove the second value as well.

I have tried to create a variable boolean restart that is set to true if the for-each loop removes the value that is less than 10. But I’m stuck.

Here is my code:

Scanner sc = new Scanner(;

    int numberOfMeasurements = sc.nextInt();
    boolean restart = false;

    ArrayList<Integer> measurements = new ArrayList<Integer>();

    for(int i = 0; i < numberOfMeasurements; i++) { 

    ArrayList<Integer> measurementsTwo = new ArrayList<>();
    for (int i : measurements) {
        if (i > 10) {
            restart = true;
    measurements = measurementsTwo;


If the description is imprecise just say it, and I will try to clarify my problem.



A good old iterator should do:

for (Iterator<Integer> it = measurements.iterator(); it.hasNext();) {
    if ( < 10) {
        if (it.hasNext()) { // ensures no failure if `it` was at last element