연락처 관리 애플리케이션 구현하기
여러분은 ArrayList로 사용자 연락처 목록을 관리하는 연락처 관리 애플리케이션을 개발하고 있습니다. 검색 함수의 성능을 구현한 뒤 분석하여, 연락처 수가 늘어날 때에도 잘 확장되는지 판단해야 합니다.
이 연습은 강의의 일부입니다
Java 코드 최적화
연습 안내
numberOfContacts를1000으로 설정하세요.- for-each 루프를 사용해
contacts리스트의 각contact를 순회하세요. findContact메서드가 찾으면 해당 contact를 반환하세요.- 코드를 그대로 실행(Run Code 버튼 사용)한 뒤,
numberOfContacts를10000으로 변경하고 다시 실행하여 실행 시간이 어떻게 달라지는지 관찰하세요. 그다음 답변을 제출하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
public class Main {
public static void main(String[] args) {
ContactManager manager = new ContactManager();
// Edit numberOfContacts to see how it affects execution time
int numberOfContacts = ____;
for (int i = 0; i < numberOfContacts; i++) {
manager.addContact(new Contact("Contact_" + i));
}
Contact result = manager.findContact("Contact_" + (numberOfContacts - 1));
System.out.println("Found: " + result.getName());
}
}
public class ContactManager {
private ArrayList contacts;
public ContactManager() {
contacts = new ArrayList<>();
}
public void addContact(Contact contact) {
contacts.add(contact);
}
public Contact findContact(String name) {
// Complete loop to search through contacts
for (Contact ____ : ____) {
if (contact.getName().equals(name)) {
// Return the match
return ____;
}
}
return null;
}
}