LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Data Types and Exceptions in Java

Kurs anzeigen

Anleitung zur Übung

  • Add "Lima", "Bogota", "Santiago" to the capitals ArrayList.
  • Convert the array in the euCapitals variable to a List.
  • Sort capitals alphabetically.
  • Reverse the order of capitals

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

import java.util.*;

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
        ____;
        
		System.out.println(capitals);
		System.out.println(euCapitalsList);
		System.out.println(euCapitals);
	}
}
Code bearbeiten und ausführen