2012-06-22 67 views
0

我正在嘗試在此數組中添加ENTRY類型的元素。但在添加之前,我想確保它不是重複的,因爲我不想重複條目。試了很多,但它保持不添加條目:/我試圖從IF子句BREAK以及似乎沒有工作。有什麼我真的很傻嗎?如果有人能幫助我,我會很感激。乾杯將非重複元素添加到可擴展陣列

import java.util.Arrays; 

public class PhoneDirectory { 
    private Entry[] ar; 
    static int index = 0; 

    public PhoneDirectory() { 
     ar = new Entry[0]; 
    } 

    public PhoneDirectory(Entry[] ent) { 
     ar = ent; 
    } 

    public boolean addEntry(Entry ent) throws NullPointerException { 

     boolean newEntry = true; 
     for (int i = 0; i < ar.length; i++) { 

      if (ar[i] == ent) { 
       newEntry = false; 

      else 
       newEntry = true; 
     } 

     int length = ar.length; 
     Entry[] temp = new Entry[length]; 

     for (int x = 0; x <= length - 1; x++) { 

      temp[x] = ar[x]; 
     } 

     length++; 
     ar = new Entry[length]; 

     // add the new entry in the last index 
     for (int i = length - 1; i >= 0; i--) { 

      if (newEntry == true) { 

       ar[i] = ent; 
       newEntry = false; 
       // return true; 
      } else { 

       // ar[i] = temp[i]; 
       return false; 
      } 
     } 

     return true; 

    } 
} 
+0

你如果塊括號是不均衡的。請更正它。 –

回答

0

你可以使用Set,動態地添加數據,並保持唯一性

Set<Entry> entries = new HashSet<Entry>(); 
//note you will have to implment `equals()` & `hashcode()` in `Entry` class 
//and then simply 
entries.add(entry1); 
entries.add(entry2); 
entries.add(entry3); 

它生長由它的自我,你不需要處理它+它保持獨特性,

如果你想保存訂單中的設置,那麼你需要使用LinkedHashSet

在你的代碼

for (int i = 0; i < ar.length; i++) { 

     if (ar[i] == ent) { 
      newEntry = false; 

     else 
      newEntry = true; 
    } 

,所以你需要在你的Entry類重寫equals(),然後你需要使用ar[i].equals(ent)檢查(而不是==

也 例如耳鼻喉科與索引值完全匹配您比較兩個對象2和數組的大小爲4,然後在最後一個索引newEntry將被設置爲true,那麼它將使這個條目,使用break;當你發現已經存在的項目

+0

我知道我可以使用Java集合,但我想通過Array來完成。我確實嘗試了休息,但似乎還有其他錯誤,我似乎無法找到。 – Achilles

+0

檢查yoru的括號是否爲 –

+0

應該使用.equals方法進行比較而不是==。請更正您的答案。 –

0

在addEntry的功能,你所創建的臨時數組中的所有先前的entrie在電話簿中。然後你重新分配了一個新的數組。但是,您從未重新填充存儲在temp中的以前條目的新ar。

length++; 
ar = new Entry[length]; 

//copy the temp entries back into the new ar 
for (int i=0; i<= temp.length-1; i++){ 
    ar[i] = temp[i]; 
} 

而且你需要在你的newEntry環加一休:

boolean newEntry = true; 
for (int i = 0; i < ar.length; i++) { 

    //comparing object should use equals 
    if (ar[i].equals(ent)) { 
     newEntry = false; 
     //add break; 
     break; 
    } 
    else 
     newEntry = true; 
} 

我不認爲你需要一個for循環中添加最新的入門

相反的:

for (int i = length - 1; i >= 0; i--) { 

     if (newEntry == true) { 

      ar[i] = ent; 
      newEntry = false; 
      // return true; 
     } else { 

      // ar[i] = temp[i]; 
      return false; 
     } 
    } 

試試這個

int i = length-1; 

if (newEntry == true) { 
    ar[i] = ent; 
    newEntry = false; 
    return true; 
} else { 
    return false; 
} 
+0

我做了這兩個,但它仍然沒有添加條目到陣列。 – Achilles

0

這是你的問題:

if (ar[i] == ent) { 
     newEntry = false; 

    else 
     newEntry = true; 

你真的想:

if (ar[i].equals(ent)) { 

這是不可能的條目將被==每個,這意味着相同的實例,而不是僅僅等同對象。

+0

此外,本節中的else子句完全沒有必要,因爲newEntry在初始化時被賦值爲true;唯一的辦法可能是false,如果它被if語句設置。 – MattS

+0

if(ar [i] .equals(ent))我將它固定到你的建議,並給它一個NullPointerException。我想加入if(ar [i] .equals(ent)&& ar [i]!= null)來查看條目是否爲空...? – Achilles

0

確保您已經覆蓋了equals方法的類條目

for (int i = 0; i < ar.length; i++) { 

    if (ar[i].equals(ent)) { 
     newEntry = false; 
     break; 
    } 
    else 
     newEntry = true; 
} 
+0

你是說重寫?或者就像你說的那樣?因爲我確實有機會==爲EQUALS,但沒有任何改變。 – Achilles

+0

是的,我的意思是覆蓋。我會糾正我的答案。 –