ArrayList trong Java được sử dụng để lưu trữ bộ sưu tập các phần tử có kích thước động.Trái ngược với Mảng được cố định về kích thước, một ArrayList tự động tăng kích thước của nó khi các phần tử mới được thêm vào nó.
ArrayList là một phần của Collection Framework của Java và thực hiệnListinterfacecủa Java.
Sau đây là một vài điểm chính cần lưu ý về ArrayList trong Java -
- Một ArrayList là một mảng có thể resize, còn được gọi là một mảng động.Nó tăng kích thước của nó để chứa các phần tử mới và thu nhỏ kích thước khi các phần tử được loại bỏ.
- ArrayList bên trong sử dụng một mảng để lưu trữ các phần tử.Giống như mảng, Nó cho phép bạn lấy các phần tử theo chỉ mục của chúng.
- Java ArrayList cho phép các giá trị trùng lặp và null.
- Java ArrayList là một collection theo thứ tự.Nó duy trì thứ tự chèn của các phần tử.
- Bạn không thể tạo một ArrayList với loại dữ liệu nguyên thủy nhưint,charBạn cần phải sử dụng các các kiểu dữ liệu tham chiếu nhưInteger,Character,Boolean,
1. Tạo một ArrayList và thêm các phần tử mới cho nó
Ví dụ này cho thấy:
- Cách tạo một ArrayList bằng cách sử dụng constructorArrayList[]để khởi tạo.
- Thêm các phần tử mới vào một ArrayList bằng phương thứcadd[].
2. Tạo một ArrayList từ Collection khác
Ví dụ này cho thấy:
- Cách tạo ArrayList từ collectionkhác bằng cách sử dụng hàmArrayList[Collection c]tạo.
- Cách thêm tất cả các phần tử từ một collectionhiện có vào ArrayList mới bằngaddAll[]phương thức.
3. Truy cập các phần tử từ một ArrayList
Ví dụ này cho thấy:
- Làm thế nào để kiểm tra nếu một ArrayList trống bằng cách sử dụngisEmpty[]phương thức.
- Cách tìm kích thước của một ArrayList bằngsize[]phương thức.
- Cách truy cập phần tử tại một chỉ mục cụ thể trong ArrayList bằngget[]phương thức.
- Cách sửa đổi phần tử tại một chỉ mục cụ thể trong ArrayList bằngset[]phương thức.
4. Xóa các phần tử khỏi một ArrayList
Ví dụ này cho thấy:
- Cách loại bỏ phần tử tại một chỉ mục đã cho trong ArrayList |remove[int index]
- Cách xóa phần tử khỏi ArrayList |remove[Object o]
- Cách xóa tất cả các thành phần khỏi ArrayList tồn tại trong một collectionđã cho |removeAll[]
- Làm thế nào để xóa bỏ các phần tử thỏa mãn một điều kiện nào đó|removeIf []
- Làm thế nào để xóa một ArrayList |clear[]
5. Duyệt qua các phần tử trong ArrayList
Ví dụ sau đây cho thấy cách duyệt qua ArrayList bằng cách sử dụng
- forEachvà biểu thức lambda.
- iterator[].
- listIterator[].
- Vòng lặp for-each
- Vòng lặp với chỉ số
8. Sắp xếp một ArrayList
Sắp xếp một ArrayList là một nhiệm vụ rất phổ biến mà bạn sẽ gặp trong các chương trình của mình.Trong phần này, tôi sẽ chỉ cho bạn cách -
- Sắp xếp một ArrayList bằngCollections.sort[]phương thức.
- Sắp xếp một ArrayList bằngArrayList.sort[]phương thức.
- Sắp xếp một ArrayList của các đối tượng do người dùng xác định bằng một bộ so sánh tùy chỉnh.
8.1. Sắp xếp một ArrayList bằng phương thức Collections.sort []
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ArrayListCollectionsSortExample { public static void main[String[] args] { List numbers = new ArrayList[]; numbers.add[13]; numbers.add[7]; numbers.add[18]; numbers.add[5]; numbers.add[2]; System.out.println["Before : " + numbers]; // Sorting an ArrayList using Collections.sort[] method Collections.sort[numbers]; System.out.println["After : " + numbers]; } } # Output Before : [13, 7, 18, 5, 2] After : [2, 5, 7, 13, 18]8.2 Sắp xếp một ArrayList bằng phương thức ArrayList.sort []
import java.util.ArrayList; import java.util.Comparator; import java.util.List; public class ArrayListSortExample { public static void main[String[] args] { List names = new ArrayList[]; names.add["Lisa"]; names.add["Jennifer"]; names.add["Mark"]; names.add["David"]; System.out.println["Names : " + names]; // Sort an ArrayList using its sort[] method. You must pass a Comparator to the ArrayList.sort[] method. names.sort[new Comparator[] { @Override public int compare[String name1, String name2] { return name1.compareTo[name2]; } }]; // The above `sort[]` method call can also be written simply using lambda expression names.sort[[name1, name2] -> name1.compareTo[name2]]; // Following is an even more concise solution names.sort[Comparator.naturalOrder[]]; System.out.println["Sorted Names : " + names]; } } # Output Names : [Lisa, Jennifer, Mark, David] Sorted Names : [David, Jennifer, Lisa, Mark]8.3 Sắp xếp một ArrayList của các đối tượng bằng cách sử dụng Trình so sánh tùy chỉnh
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; class Person { private String name; private Integer age; public Person[String name, Integer age] { this.name = name; this.age = age; } public String getName[] { return name; } public void setName[String name] { this.name = name; } public Integer getAge[] { return age; } public void setAge[Integer age] { this.age = age; } @Override public String toString[] { return "{" + "name='" + name + '\'' + ", age=" + age + '}'; } } public class ArrayListObjectSortExample { public static void main[String[] args] { List people = new ArrayList[]; people.add[new Person["Sachin", 47]]; people.add[new Person["Chris", 34]]; people.add[new Person["Rajeev", 25]]; people.add[new Person["David", 31]]; System.out.println["Person List : " + people]; // Sort People by their Age people.sort[[person1, person2] -> { return person1.getAge[] - person2.getAge[]; }]; // A more concise way of writing the above sorting function people.sort[Comparator.comparingInt[Person::getAge]]; System.out.println["Sorted Person List by Age : " + people]; // You can also sort using Collections.sort[] method by passing the custom Comparator Collections.sort[people, Comparator.comparing[Person::getName]]; System.out.println["Sorted Person List by Name : " + people]; } } # Output Person List : [{name='Sachin', age=47}, {name='Chris', age=34}, {name='Rajeev', age=25}, {name='David', age=31}] Sorted Person List by Age : [{name='Rajeev', age=25}, {name='David', age=31}, {name='Chris', age=34}, {name='Sachin', age=47}] Sorted Person List by Name : [{name='Chris', age=34}, {name='David', age=31}, {name='Rajeev', age=25}, {name='Sachin', age=47}]Phần kết luận
Đó là tất cả mọi thứ tôi chia sẻ về ArrayList trong Java.Trong bài viết này, bạn đã tìm hiểu ArrayList là gì, cách tạo ArrayList, cách thêm, sửa đổi và xóa các phần tử khỏi ArrayList, cách lặp qua ArrayList, cách sắp xếp ArrayList và cách đồng bộ hóa quyền truy cập vào ArrayList .
Cảm ơn bạn đã đọc.Hẹn gặp lại trong bài tiếp theo.