Contacts मैनेजमेंट एप्लिकेशन लागू करना
आप एक contacts मैनेजमेंट एप्लिकेशन बना रहे हैं जो ArrayList का उपयोग करके यूज़र contacts की सूची रखता है। आपको अपनी search फंक्शन को implement करना है और फिर उसके performance का विश्लेषण करना है ताकि पता चल सके कि contacts की संख्या बढ़ने पर यह कितनी अच्छी तरह scale करेगा।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में कोड ऑप्टिमाइज़ करना
अभ्यास निर्देश
numberOfContactsको1000पर सेट करें।contactsसूची में हरcontactपर iterate करने के लिए for-each loop का उपयोग करें।- जब
findContactमेथड उसे ढूँढ ले, तो वही contact return करें। - पहले कोड को ज्यों का त्यों चलाएँ ("Run Code" बटन का उपयोग करके), फिर
numberOfContactsको10000में बदलें और दोबारा चलाएँ, और देखें कि execution time कैसे बदलता है। इसके बाद अपना उत्तर सबमिट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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;
}
}