Sorting a time intervals

后端 未结 5 869
逝去的感伤
逝去的感伤 2021-01-27 12:25

I am giving a time interval in the form of two arrays.

A[0]=  2  B[0]=3
A[1]=  9  B[1]=11
A[2] = 5 B[2]=6
A[3] = 3 B[3]=10

I want to sort the i

5条回答
  •  迷失自我
    2021-01-27 13:02

    Step 1: Java is an object oriented language; learn to use objects.

    Possible class for the time interval

    public class TimeInterval implements Comparable
    {
        private int end;
        private int start;
    
        public TimeInterval(
            final int end,
            final int start)
        {
            this.end = end;
            this.start = start;
        }
    
        public int getEnd()
        {
            return end;
        }
    
        public int getStart()
        {
            return start;
        }
    
        public int comareTo(final TimeInterval other)
        {
            if (other == null)
            {
                return -1; // this will put the null value objects at the end.
            }
    
            return start - other.start;
        }
    }
    

提交回复
热议问题