Using Collections and Arrays
In this exercise, you will see how the Collections Framework supporting classes of Collections and Arrays can help you manipulate and work with data more easily.
This exercise is part of the course
Data Types and Exceptions in Java
Exercise instructions
- Add
"Lima","Bogota","Santiago"to thecapitalsArrayList. - Convert the array in the
euCapitalsvariable to aList. - Sort
capitalsalphabetically. - Reverse the order of
capitals
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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);
}
}