序列化问题

时间:2017-02-26 21:50:48

标签: serialization calendar

目前我正在尝试读取和写入futureMeetingArray和pastMeetingArray,然后在getMeetingListOn方法中调用它们。此方法应查找具有相同日期的任何会议,并将它们添加到数组并返回该数组。但是,目前它还没有找到这些(到目前为止我只是在futureMeetingsArray上测试它)。我还添加了addFutureMeeting方法来说明我如何设置添加然后序列化futureArrayList。每个FutureMeeting对象都有一个全局Calendar变量,我通过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;
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定&#34;序列化&#34;问题你在这里。但我发现你正在使用&#34; ==&#34;比较对象。您应该使用.equals或尝试使用比较器。

请参阅此帖子,了解您不应该使用的原因&#34; ==&#34;。 Compare two objects with .equals() and == operator