BaşlayınÜcretsiz Başlayın

Updating names in a contact list

A contact management system stores names in a list. When a name needs to be updated (e.g., correcting typos), you can use ListIterator to modify entries while iterating.

All the necessary classes from java.util have been imported for you.

Bu egzersiz

Input/Output and Streams in Java

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Create a ListIterator object for the contacts list.
  • In the condition of the while loop, check if more elements are available in the contacts list.
  • Retrieve next element.
  • If a name is Jon, set it to John instead.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

public class ContactUpdater {
    public static void main(String[] args) {
        ArrayList contacts = new ArrayList<>();
        contacts.add("Alice");
        contacts.add("Jon");
        contacts.add("Charlie");

        // Create ListIterator for the contacts list
        ListIterator it = contacts.____();

        // Check if more elements are available
        while (____.____()) {
        	// Retrieve next element
            String name = ____.____();
            if (name.equals("Jon")) {
                // Update element to "John"
                ____.____("John");
            }
        }

        System.out.println(contacts);
    }
}
Kodu Düzenle ve Çalıştır