2017-02-26 190 views
0

目前我試圖讀寫futureMeetingArray和pastMeetingArray再後來在getMeetingListOn方法請他們發言。此方法應查找具有相同日期的任何會議並將它們添加到數組並返回該數組。然而,目前它沒有找到這些(到目前爲止,我只在futureMeetingsArray上測試它)。我還包括了addFutureMeeting方法來說明我已經設置瞭如何添加並序列化futureArrayList。每個FutureMeeting對象都有一個全局日曆變量,我通過getter和setter調用它。如果它更清晰,我可以提供此代碼。序列化問題

如果任何人都可以提出一個原因是沒有找到的日期,我將不勝感激。

public class ContactManagerImpl implements ContactManager { 
    Calendar date; 
    List<Meeting> futureMeetingArray = new ArrayList<Meeting>(); 
    List<PastMeetingImpl> pastMeetingArray = new ArrayList<PastMeetingImpl>(); 
    List<Contact> allContacts = new ArrayList<Contact>(); 

    @Override 
    public int addFutureMeeting(Set<Contact> contacts, Calendar date) { 
     readSerializedData(); 
     int ID = (int)(Math.random()*500 + 1000); 
     FutureMeetingImpl fm = new FutureMeetingImpl(contacts, ID, date); 
     futureMeetingArray.add(fm); 
     makeNewFolder(); 
     try { 
      FileOutputStream fileOut = new FileOutputStream("./serialized/newfuturemeeting1.out"); 
      ObjectOutputStream out = new ObjectOutputStream(fileOut); 
      out.writeObject(futureMeetingArray); //This is so I can use later methods down where you need to get specific meetings in list form 
      out.close(); 
      fileOut.close(); 
     }catch(IOException i) { 
      i.printStackTrace(); 
     } 
      contactAdder(contacts); 
     return ID; 
    } 

    @Override 
    public List<Meeting> getMeetingListOn(Calendar date) { 
     readSerializedData(); 
     SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); 
     String formatted = format1.format(date.getTime()); 
     List<Meeting> meetings = new ArrayList<Meeting>(); 
     Meeting currentMeeting = futureMeetingArray.get(0); 
     if(futureMeetingArray.size() > 0){ 
      for(int x = 0; x < futureMeetingArray.size(); x++){ 
       currentMeeting = futureMeetingArray.get(x); 
       Calendar currentMeetingsDate = currentMeeting.getDate(); 
       System.out.println("s"); 
       if(currentMeetingsDate == date){ 
        System.out.println("Adding future"); 
        meetings.add(currentMeeting); 
       } 
      } 
     } 

      for (int p = 0; p < pastMeetingArray.size(); p++){ 
       Meeting currentMeetingPast = new PastMeetingImpl(); 
       SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd"); 
       String formatted3 = format3.format(currentMeetingPast.getDate()); 
       System.out.println(formatted3); 
       if(formatted3 == formatted){ 
        System.out.println("Adding future"); 
        meetings.add(currentMeetingPast); 
       } 
      } 
     for(int y = 0; y < meetings.size(); y++){ 
      Meeting meetingPrint = meetings.get(y); 
      System.out.println("Your list of meetings include meeting IDs: " + meetingPrint.getId()); 
     } 
     return meetings; 
    } 
} 

+0

這可能與序列化問題 - 我可能無法正確使用Calendar類。如果有人有任何關於使用這個信息,我會很感激 – BitLord

回答

0

我不知道您有什麼在這裏「系列化」的問題。但我看到你正在使用「==」來比較對象。您應該使用.equals或嘗試使用比較器。

請參閱這篇文章爲什麼你不應該使用「==」。 Compare two objects with .equals() and == operator

+0

謝謝,我會修改這個。不過,我仍然得到相同的結果,只有循環在futureMeetingArray中執行,而不是添加任何內容。我使用的測試是這樣的: '@Test \t公共無效testgetMeetingListOn(){ \t \t的System.out.println( 「打印日期」 + this.date); \t \t cm.getMeetingListOn(this.date); \t}' – BitLord

+0

你添加日期時使用相同的日期格式,時區和語言環境(是否你傳遞給addFutureMeeting()日曆實例是相同的格式,時區和語言環境,你是比較反對?日期)如果您使用「.equals」來比較,那麼你比較的兩個日期應該是相同的,包括毫秒。如果您不想比較時間戳,請分別比較年份,月份和日期。 – BadeMan