Sử dụng Collections và Arrays
Trong bài tập này, bạn sẽ thấy các lớp hỗ trợ của Collections Framework là Collections và Arrays có thể giúp bạn thao tác và làm việc với dữ liệu dễ dàng hơn như thế nào.
Bài tập này là một phần của khóa học
Kiểu dữ liệu và Exceptions trong Java
Hướng dẫn bài tập
- Thêm
"Lima","Bogota","Santiago"vàoArrayListcapitals. - Chuyển mảng trong biến
euCapitalsthành mộtList. - Sắp xếp
capitalstheo thứ tự chữ cái. - Đảo ngược thứ tự của
capitals
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
public class AllTheCities {
public static void main(String[] args) {
String[] euCapitals = {"Paris", "London", "Prague"};
ArrayList capitals = new ArrayList();
// Add cities to capitals all at once
Collections.____(____, "Lima", "Bogota", "Santiago");
// Convert euCapitals to List
____<____> euCapitalsList = Arrays.____(euCapitals);
// Sort capitals
____.____(capitals);
// Reverse capitals
Collections.____(____);
System.out.println(capitals);
System.out.println(euCapitalsList);
System.out.println(euCapitals);
}
}