연락처 목록에서 이름 업데이트하기
연락처 관리 시스템은 이름을 목록에 저장합니다. 이름을 업데이트해야 할 때(예: 오타 수정) ListIterator를 사용하면 순회하면서 항목을 수정할 수 있습니다.
필요한 java.util의 모든 클래스는 이미 가져와 두었습니다.
이 연습은 강의의 일부입니다
Java의 입력/출력과 스트림
연습 안내
contacts리스트에 대한ListIterator객체를 생성하세요.- while 루프의 조건에서
contacts리스트에 더 많은 요소가 있는지 확인하세요. - 다음 요소를 가져오세요.
- 이름이
Jon이면John으로 바꾸세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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);
}
}