简体   繁体   中英

Sorting algorithm with arrayLists

I'm trying to make a scheduling program where no two talks can proceed at the same time, although a talk can proceed as the other one ends. After putting the first talk into Scheduler plan, I want to go through each talk in my arrayList and check if there compatible with the last talk in the schedule.

I figure I can check by comparing if the startTime of index i is greater than the endtime of index i-1.

I sorted my talks and lectures by there endTime and inputted the first array. Now I'm having trouble comparing the events and adding the right ones into Scheduler plan.

public class Scheduler {

private  ArrayList<Event> events = new ArrayList <Event>();

public Scheduler(ArrayList<Event> events){ //Constructor
    for (int i=0; i<events.size(); i++)
        this.events.add(events.get(i));
    }

    public ArrayList<Event> getsortSchedule(){ //Sorting Algorithm
        int N = events.size() -1;

            for (int i = 0; i <=(N-1); i++)
        {
                for(int j = 1; j <= N;j++)
                {
                        if(events.get(i).getendTime().compareTo(events.get(j).getendTime()) > 0)  
                        Collections.swap(events, i, j);
                }   
        }


        return events;
    }

    public Scheduler getSchedule(){ //Scheduling attempt
        Scheduler plan = new Scheduler(events);
        this.events.add(events.get(0));

        for (int i=0; i == events.size(); i++)
        {
            //if(events.get(i).getStartTime() > events.get(i).getendTime())
            //if(events.get(i).getStartTime().compareTo(events.get(l).getendTime()) > events.get(i-1).getendTime().compareTo(events.get(l).getendTime()))  
            this.events.add(events.get(i));
        }

        return plan;
    }

If you are using ArrayList then you can use Collections ...

dont reinvent the wheel and sort the list doing

Collections.sort(events);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM