簡體   English   中英

比較相同數組java的元素

[英]compare elements of same array java

我正在為每周一次的定期約會功能工作。 我有一個開始日期,結束日期,重復開始日期,重復結束日期和選定的天數(星期一,星期二,星期三,星期四...星期日)

這是一個例子

Start Date: 15th July 2015
End Date: 18th July 2015

Recurrence Start Date: 20th July 2015
Recurrence End Date: 20th August 2015

Recurrence frequency = weekly

Selected Days(Array storing int values for days_of_week with Sun as 1 and Sat as 7) = Mon, Wed, Sun 

根據要求,我需要創建一個像這樣的約會:

Appointment 1 - 20th July 2015 (Mon) - 23rd July 2015(Thu)
............Appointment 2 - 22nd July 2015 (Wed)  - 25th July 2015(Sat)
............Appointment 3 - 26th July 2015 (Sun) - 29th July 2015(Wed)
............Appointment 4 - 27th July 2015 (Mon) - 30th July 2015(Thu)

但是正如您所看到的,我需要防止重疊。 我試圖開發一種算法來防止這種重疊,而實際上並不知道實際的日子。

所以基本上沒有。 開始日期和結束日期之間的天數必須大於數組的兩個連續索引之間的差。

我遇到了問題,因為Sun - Wed ( 1 - 4)會給我一個負數,因此比較將小於結束日期和開始日期( end date - start date)之間的天差。

這是我到目前為止所做的:

                        Calendar e = Calendar.getInstance();
                        Calendar f = Calendar.getInstance();
                        e.setTime(sStartDate);
                        f.setTime(estSignIn);

                        long diffInDays = ((estSignIn.getTime() - sStartDate.getTime()) 
                                / (1000 * 60 * 60 * 24) );


                        for(int j=0; j < localSelectedDays.length - 1 ; j++)
                        {   
                            e.set(Calendar.DAY_OF_WEEK, localSelectedDays[j]);
                            f.set(Calendar.DAY_OF_WEEK, localSelectedDays[j +1]);
                             int x = e.get(Calendar.DAY_OF_WEEK);
                             int y = f.get(Calendar.DAY_OF_WEEK);

                                 if ((y - x) <= diffInDays)
                                  { 
                                    System.out.println("ERROR" + "Y:" + y + "x" + x);
                                  }
                        }

如果只需要天數差異,則可以使用Math.abs()。

我個人不會將索引號用作算法的一部分,因為有一天您可能想更改數據結構,但這是主觀的。

但是正如您所看到的,我需要防止重疊。 我試圖開發一種算法來防止這種重疊,而實際上並不知道實際的日子

我已經根據您的要求編寫了一些代碼。 我認為使用Calendar或Date類的afterbefore方法來比較輸入日期,而不是使用數組比較它們。

創建一個類AppointmentDetails,它將存儲約會詳細信息。 您可以在其中添加其他屬性,例如人物姓名等。

public class AppointmentDetails {

    private Calendar startDate;
    private Calendar endDate;
   //getters & setters
     public String toString()
      {
       return "startDate "+ this.getStartDate() + "endDate " + this.getEndDate();
      }

在客戶端類中,將約會詳細信息存儲在Hashmap中

public class AppointmentClient {


    public static void main(String[] arg)
    {
        System.out.println("Get the appointment");
        //Sample data input for the first appointment
        Calendar startDate = new GregorianCalendar(2015, 1, 6);
        Calendar endDate = new GregorianCalendar(2015, 1, 9);

        AppointmentDetails details = new AppointmentDetails();
        details.setStartDate(startDate);
        details.setEndDate(endDate);

        //Details added to the hashmap with key as appointment & value as the class holding appointment details
        Map<String,AppointmentDetails> map = new    HashMap<String,AppointmentDetails>();
        map.put("Appointment 1", details);

        //Dates for new appointment
        Calendar startDate2 = new GregorianCalendar(2015, 1, 7);
        Calendar endDate2 = new GregorianCalendar(2015, 1, 9);

            //logic for validating if the appointment on the input date is already booked
            for(Map.Entry<String, AppointmentDetails> appDtl:map.entrySet())
            {
                System.out.println("Inside loop");
                //Every time the loop iterates it will return the respective appointment
                AppointmentDetails apptDetail = appDtl.getValue();
                System.out.println(apptDetail);
                if(((apptDetail.getStartDate().equals(startDate2))||(apptDetail.getEndDate().equals(endDate2)))|| 
                        (apptDetail.getStartDate().after(startDate2))&&(apptDetail.getEndDate().before(endDate2)))
                {
                     System.out.println("Your appointment is overlapping with the existing appointments");
                     break;
                }
            }
         }
      }

PS這只是一個基本代碼,您可以進一步自定義它,以提高效率和邏輯性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM