Published 16 Oct, 2022

Java - Delete numbers larger than x in an ArrayList

Category Java
Modified : Nov 28, 2022
51

I'm a noob in java and right now I'm learning about generics. This code should delete any integer larger than five. I typed [10, 11, 12, 1], in theory, I should only get [3, 4, 6, 1]. But I'm getting [3, 4, 6, 11, 1], I don't understand why..?

public static void main(String args[]) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(3);
    list.add(4);
    list.add(56);
    list.add(6);

    for (int i = 0; i < 4; i++){
        String s = reader.readLine();
        list.add(Integer.parseInt(s));
    }

    for (int i = 0; i < list.size(); i++){
        if (list.get(i) > 5)
            list.remove(i);
        //else
            //i++;
    }
    System.out.println(list);
}

10 11 12 1

[3, 4, 6, 11, 1]

Answers

There are 4 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Java, Generics, Arraylist. These have been categorized in sections for a clear and precise explanation.

41

If you read the documentation for List.remove(), you will see that the elements after index i are shifted to the left. This means that in your current implementation, you are skipping one element after each removal, so if there are two elements next to each other that are larger than 5, only one of them will be removed.

What you can do is to also shift back the current index one step after you remove an element with i--;. Your code would thus become

public static void main(String args[]) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(3);
    list.add(4);
    list.add(56);
    list.add(6);

    for (int i = 0; i < 4; i++){
        String s = reader.readLine();
        list.add(Integer.parseInt(s));
    }

    for (int i = 0; i < list.size(); i++){
        if (list.get(i) > 5) {
            list.remove(i);
            i--;
        }
    }
    System.out.println(list);
}

This will output [3, 4, 1], removing all numbers that are larger than 5.


52

If you are using Java 8+, You can just use ArrayList::removeIf like so :

list.removeIf(i -> i > 5);

ideone demo


16

If you start the check from end of the array, it's working.

public static void main(String args[]) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();

    list.add(3);
    list.add(4);
    list.add(56);
    list.add(6);

    for (int i = 0; i < 4; i++){
        String s = reader.readLine();
        list.add(Integer.parseInt(s));
    }

    for (int i = list.size() - 1; i >=0 ; i--){
        if (list.get(i) > 5)
            list.remove(i);
    }
    System.out.println(list);
}

10 11 12 1
[3, 4, 1]


2

I imagine that was the exercise to understand how to remove from a list, but otherwise, this is how you could check before adding to it:

  public static void main(String args[]) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    ArrayList<Integer> list = new ArrayList<Integer>();



    for (int i = 0; i < 4; i++){
        System.out.println("type a number: ");
        String s = reader.readLine();
        int j = Integer.parseInt(s);
        if(j < 5) {
            list.add(Integer.parseInt(s));
        }
    }


    System.out.println(list);
}