2013-04-05 38 views
-1

它應該保持Contact_List按姓氏的字母順序。我一直在這裏待了好幾個小時,卻無法弄清楚。我在帖子中添加了add方法和findContactIndex方法,該方法應該可以找到聯繫人的索引。我不斷收到空指針異常和其他錯誤。我需要做的就是找出它按字母順序排列的位置,然後將新的聯繫人插入並將其移回,以便它們都按正確的順序排列。請幫助修復我的添加方法

public void add(Contact frnd) 
{ 
    if(numContacts == 0) 
    { 
     Contact_List[0] = frnd; 
    } 
    else 
    { 
     for(int i = 0; i < numContacts; i++) 
     { 
      int x =Contact_List[i].getLastName().compareToIgnoreCase(frnd.getLastName()); 
      if(x < 0) 
      { 
      Contact_List[findContactIndex(Contact_List[i].getLastName())] = frnd; 
      break; 
      } 
     } 
    } 
    numContacts++; 
    } 


    /** 
    * Searches the Contact_List to find a contact by last name 
    * @param lstnm The last name of a Contact to be found 
    * @return indexOfContact The index of the contact that has been found 
    */ 
    public int findContactIndex(String lstnm) 
    { 
     int indexOfContact = -1; // Defaults it at 0 incase the name is not found 
     for(int i = 0; i < numContacts; i++) // Once for every Contact 
     { 
      int a = lstnm.compareTo(Contact_List[i].getLastName()); 
      if(a == 0) // If the Contact is found 
      { 
       indexOfContact = i; // Assign the index of that Contact 
      } 
     } 
     return indexOfContact;  
} 
+3

你在哪裏初始化你的數組?也許你應該使用一個集合? – 2013-04-05 06:31:34

+0

我必須使用一個數組,並且它在類的前面初始化 – 2013-04-05 06:32:48

+0

那麼尺寸管理呢? - 是否給出了聯繫人總數的大小?不幸的是,Java對數組有點痛苦...... – 2013-04-05 06:37:56

回答

0

下面是一些可以用於數組的代碼。以下也是測試此代碼的主要方法。

public class ContactList { 

static class Contact { 
    String lastName; 

    Contact(final String lastName) { 
     this.lastName = lastName; 
    } 

    @Override 
    public String toString() { 
     return String.format("Contact[%s]", this.lastName); 
    } 
} 

static final int MAX_CONTACTS = 10; 

Contact[] contactList = new Contact[MAX_CONTACTS]; 

void add(final Contact newContact) { 
    if (newContact == null || newContact.lastName == null) { 
     return; 
    } 
    Contact contactToAdd = newContact; 
    for (int pos = 0; pos < MAX_CONTACTS; pos++) { 

     final Contact current = this.contactList[pos]; 
     if (current == null) { // we found the end, we're done 
      this.contactList[pos] = contactToAdd; 
      break; 
     } else if (current.lastName.compareTo(contactToAdd.lastName) > 0) { // current comes after contactToAdd 
      this.contactList[pos] = contactToAdd; 
      contactToAdd = current; 
     } 
    } 
} 

static final String[] lastNames = { "A", "C", "E", "B", "D", null }; 

public static void main(final String[] args) { 
    final ContactList contactList = new ContactList(); 
    for (final String lastName : lastNames) { 
     final Contact contact = new Contact(lastName); 
     contactList.add(contact); 
     System.out.println(Arrays.toString(contactList.contactList)); 
    } 
} 

}

+0

哇,這真的很有幫助!我只是改變了幾個名字,讓它起作用!謝謝! – 2013-04-05 07:29:13

+0

我沒有包括的是檢查contactList [MAX_CONTACTS]!= null這將意味着列表已滿,您不能添加任何聯繫人。 – Yashima 2013-04-05 07:40:04

0

你真的應該使用TreeSet

SortedSet contactList = new TreeSet<Contact>(new Comparator<Contact>() { 
    public int compare(Contact o1, Contact o2) { 
     String lastName1 = o1 == null ? null : o1.lastName; 
     String lastName2 = o2 == null ? null : o2.lastName; 
     if (lastName1 == null) { 
      return lastName2 == null ? 0 : -1; // causes Contacts without last name to be sorted to the end 
     } 
     return lastName1.compareTo(lastName2); 
    } 
}); 

void add(final Contact newContact) { 
    this.contactList.add(newContact); 
} 

不過請注意,它通常是有意義的在這種情況下,使用Collator字符串比較,因爲, 「歐米茄」將因「大寫」而在「alpha」之前排序。