Array List
Duy Văn | 12/03/2025
ArrayList chính là một trong những tinh tuý của Java mà cần phải nắm rất rõ, nó tương đương với list ở phía Python nhưng với ArrayList thì data type cho các phần tử là cố định
Nếu built-in array của java không thể thay đổi kích thước, tức là khả năng dùng nó trong Dynamic Programming là không thể, nhưng với ArrayList thì hoàn toàn có thể
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
}
Tạo nhanh ArrayList
Để tạo một arraylist nhanh chóng, chúng ta có thể dùng method Arrays.asList()
import java.util.ArrayList;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<>(Arrays.asList("Volvo", "BMW", "Ford", "Mazda"));
System.out.println(cars);
}
}
Class Arrays cung cấp một số method hữu ích cho việc làm việc với array, trong trường hợp này là asList() để chuyển một array thành một arraylist
Thêm element vào ArrayList
Để thêm một element vào arraylist, chúng ta dùng method add()
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
}
}
Đồng thời add() có thể nhận vào 2 param, index và element
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add(0, "Volvo");
}
}
Đây thay vì đẩy phần từ vào cuối arraylist, nó sẽ đẩy vào index 0
Kiểm tra element có trong ArrayList
Để kiểm tra xem một element có trong arraylist hay không, chúng ta dùng method contains()
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
System.out.println(cars.contains("Volvo"));
}
}
Truy cập element trong ArrayList
Sau đó để truy cập element trong ArrayList, chúng ta sẽ không gọi thẳng index như array mà sẽ dùng method get()
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
System.out.println(cars.get(0));
}
}
Sửa giá trị của element trong ArrayList
Tương tự với get, chúng ta dùng set để sửa giá trị phần từ tại một index
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.set(0, "Opel");
System.out.println(cars);
}
}
Bỏ một element khỏi ArrayList
Dể bỏ một giá trị khỏi arraylist thì có thể dùng method remove() với index
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.remove(0);
System.out.println(cars);
}
}
Để có thể bỏ một element theo giá trị thì remove() cũng có thể làm được
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.remove("Volvo");
System.out.println(cars);
}
}
Để clear toàn bộ ArrayList thì có thể dùng method clear()
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.clear();
System.out.println(cars);
}
}
Cỡ của ArrayList
Để lấy ra cỡ (chứa bao nhiêu phần tử) của arraylist thì dùng method size()
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
System.out.println(cars.size());
}
}
Loop qua ArrayList
Với size() thì chúng ta có thể tạo ra một vòng lặp for hoặc có thể dùng for each
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
}
}
với for each
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
for (String i : cars) {
System.out.println(i);
}
}
}
Các loại ArrayList
Có thể kể đến:
- ArrayList chứa String :
ArrayList<String> cars = new ArrayList<String>(); - ArrayList chứa Integer :
ArrayList<Integer> myNumbers = new ArrayList<Integer>(); - ArrayList chứa Double :
ArrayList<Double> myDouble = new ArrayList<Double>(); - ArrayList chứa Character :
ArrayList<Character> myChar = new ArrayList<Character>(); - ArrayList chứa Boolean :
ArrayList<Boolean> myBool = new ArrayList<Boolean>();
Sort
Để sort một arraylist thì chúng ta cần vận dụng class Collections và method sort()
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars);
System.out.println(cars);
}
}
Quy tắc của nó sẽ là sort theo thứ tự alphabet, nếu là số thì sẽ sort theo thứ tự số từ nhỏ đến lớn
Nếu muốn sort theo thứ tự ngược lại thì có thể dùng method reverseOrder()
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars, Collections.reverseOrder());
System.out.println(cars);
}
}