ArrayList remove() trong java với ví dụ cụ thể

Để xoá các phần tử ra khởi ArrayList, ArrayList cung cấp cho chúng ta một số method mà ngay bây giờ chúng ta sẽ cùng nhau tìm hiểu.

public E remove(int index)

Xoá một phần tử tại vị trí index, và trả về phần tử được xoá. Lưu ý, nếu index < 0 hoặc lớn hơn số lượng phần tử của ArrayList thì chúng ta sẽ bị IndexOutOfBoundException.

Code

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        int remove = arr.remove(0);
        System.out.println("Phan tu bi xoa: " + remove);

        System.out.print("Cac phan tu con lai: ");
        for (Integer i : arr) {
            System.out.print(i + " ");
        }
    }
}

Output:

Phan tu bi xoa: 1
Cac phan tu con lai: 2 3

public boolean remove(Object o)

Khác với method ở trên, method này sẽ xoá theo object mà chúng ta truyền vào, nó sẽ tiến hành tìm trong ArrayList và xoá đi. 

Note: remove(Object o): sẽ xoá phần tử đầu tiên được tìm thấy. Nếu object truyền vào không tìm thấy trong ArrayList thì chúng ta sẽ nhận giá trị false.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(1);
        arr.add(2);
        arr.add(1);
        arr.add(3);

        boolean isRemoved = arr.remove(Integer.valueOf("1"));
        if (isRemoved) {
            System.out.println("Xoa thanh cong");
        }
        System.out.print("Cac phan tu con lai: ");
        for (Integer i : arr) {
            System.out.print(i + " ");
        }
    }
}

Output:

Xoa thanh cong
Cac phan tu con lai: 2 1 3

Vì sao mình dùng Integer.ValueOf() để tránh trình biên dịch hiểu nhầm là mình đang gọi method ở phần đầu nhé. Nếu sau này ArrayList các bạn chứa các object tự định nghĩa như Student etc thì ko phải lo.

public boolean removeIf(Predicate<? super E> filter)

Xoá phần tử theo điều kiện cụ thể. Để hiểu hàm này, mình khuyên các bạn nên đọc về Lamdba expression trước nhé. 

Nếu điều kiện filter chúng ta không so khớp được với bất kỳ phần tử nào trong ArrayList thì method sẽ trả về false, tất là chúng ta không xoá được phần tử nào. 

Các phần tử được so khớp sẽ bị xoá toàn bộ khởi ArrayList.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {

        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(1);
        arr.add(2);
        arr.add(1);
        arr.add(3);

        boolean isRemoved = arr.removeIf(t -> t.equals(1));
        if (isRemoved) {
            System.out.println("Xoa thanh cong");
        }
        System.out.print("Cac phan tu con lai: ");
        for (Integer i : arr) {
            System.out.print(i + " ");
        }
    }
}

Output:

Xoa thanh cong
Cac phan tu con lai: 2 3

public boolean removeAll(Collection<?> c)

Xoá các phần tử cuả ArrayList dựa vào một tập collection. ArrayList sẽ tiến hành tìm kiếm các phần tử của nó mà chứa trong collection c thì sẽ xoá bỏ toàn bộ.

Nếu không có phần tử nào của ArrayList chứa trong collection c thì chúng ta nhận giá trị trả về false

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

        ArrayList<Integer> arr = new ArrayList<Integer>();
        arr.add(1);
        arr.add(2);
        arr.add(1);
        arr.add(3);

        ArrayList<Integer> tmp = new ArrayList<>();
        tmp.add(1);
        tmp.add(4);

        boolean isRemoved = arr.removeAll(tmp);
        if (isRemoved) {
            System.out.println("Xoa thanh cong");
        }
        System.out.print("Cac phan tu con lai: ");
        for (Integer i : arr) {
            System.out.print(i + " ");
        }
    }
}

Output:

Xoa thanh cong
Cac phan tu con lai: 2 3

3.3 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x