2012-04-10 16 views
5

ऑर्डर करने के लिए तुलनाकर्ता का उपयोग करना मुझे दिनांक और समय के अनुसार नियुक्तियों को ऑर्डर करना होगा। मेरे पास नियुक्तियों का एक ऐरेलिस्ट है और उन्होंने अपनी तिथियों और समय की तुलना करने के लिए एक तुलनित्र बनाने की कोशिश की है। मैं Collections.sort विधि का उपयोग करने की कोशिश कर रहा हूं, इसे अपॉइंटमेंट्स की ऐरेलिस्ट और मेरे द्वारा बनाए गए अपॉइंटमेंट कॉम्पैटर को पास कर रहा हूं। संकलन करते समय मुझे "सॉर्ट करने के लिए कोई उपयुक्त विधि" नहीं मिलती है। यहाँ संकलक द्वारा उत्पन्न पूर्ण त्रुटि संदेश के लिए एक लिंक है: http://prntscr.com/7y4qbArrayList Java

तुलनाकारी: सिंटैक्स त्रुटि वाला

public class AppointmentComparator implements Comparator<Appointment> 
{ 
public int compare(Appointment a, Appointment b) 
{ 
    if (a.getDay() < b.getDay()) 
     return -1; 

    if (a.getDay() == b.getDay()) 
    { 
     if (a.getStart() < b.getStart()) 
      return -1; 
     if (a.getStart() > b.getStart()) 
      return 1; 
     return 0; 
    } 

    return 1; 
} 

पंक्ति:

Collections.sort(book, new AppointmentComparator()); 

चर पुस्तक नियुक्ति की एक ArrayList है। ArrayList<Appointment>

AppointmentBook वर्ग:

import java.util.ArrayList; 
import java.util.Collections; 

public class AppointmentBook 
{ 
private ArrayList<Appointment> book; 

public AppointmentBook() 
{ 
    book = new ArrayList<Appointment>(); 
} 

public void addAppointment(Appointment appt) 
{ 
    book.add(appt); 
    Collections.sort(book, new AppointmentComparator()); 
} 

public String printAppointments(int day) 
{ 
    String list = ""; 

    for (int i = 0; i < book.size(); i++) 
    { 
     if (book.get(i).getDay() == day) 
     { 
      list = list + "Appointment description: " + book.get(i).getDescription() + "\n" + "Date of Appointment: " + 
      book.get(i).getDay() + "\n" + "Time: " + book.get(i).getStart() + " - " + book.get(i).getEnd() + "\n" + "\n"; 
     } 
    } 

    return list; 
} 

नियुक्ति वर्ग:

public class Appointment 
{ 
private String desc; 
private int day; //in format mmddyyyy 
private int start; //in format hhmm 
private int end; //in format hhmm 

public Appointment(String description, int aptDay, int startTime, int endTime) 
{ 
    desc = description; 
    day = aptDay; 
    start = startTime; 
    end = endTime; 
} 

public String getDescription() 
{ 
    return desc; 
} 

public int getDay() 
{ 
    return day; 
} 

public int getStart() 
{ 
    return start; 
} 

public int getEnd() 
{ 
    return end; 
} 

}

+1

क्या आप हमारे लिए संपूर्ण अपॉइंटमेंट कॉम्परेटर कक्षा पोस्ट कर सकते हैं? कक्षा परिभाषा में स्वयं एक समस्या हो सकती है, और हम केवल संकलक त्रुटि/कक्षा परिभाषा के बिना अनुमान लगा सकते हैं। –

+1

क्या आप यह भी पोस्ट कर सकते हैं कि आपने Collections.sort() को कॉल करने से पहले पुस्तक चर को कैसे परिभाषित किया है? –

+0

पूरी कक्षा – Legendish

उत्तर

3

त्रुटि संदेश ऐसा लगता है कि आप इंटरफ़ेस को लागू करने के रूप में अपने तुलनित्र घोषित करने के लिए भूल गया से:

public class AppointmentComparator implements Comparator<Appointment> {} 

इसमें उपकरण शामिल नहीं होना चाहिए, न केवल विधि शामिल है।

+0

क्षमा करें मैंने शुरुआत में इसे जोड़ा नहीं था, लेकिन हाँ यह वहां था और मुझे अभी भी त्रुटि मिली है – Legendish

+0

@ user1323008 - सबकुछ पुनः संकलित करें? यदि नहीं, तो पूर्ण कोड –

+0

पोस्ट करें, मैं पूर्ण कोड को अपॉइंटमेंटबुक क्लास पर पोस्ट करता हूं, जहां मुझे त्रुटि हो रही है। मुझे यकीन है कि तुलनाकर्ता सही ढंग से किया जाता है। – Legendish

0

लगता है कि आप अपने AppointmentComparator के लिए तुलनित्र इंटरफ़ेस लागू नहीं किया है

1

आप

Collections.sort(book, new (Comparator)AppointmentComparator()); 
0

इसके अलावा हम कुछ मामलों के लिए भीतरी वर्ग का उपयोग कर सकते अपने नए AppointmentComparator कास्ट करने के लिए की जरूरत है:

public int indexOfLargest(ArrayList<QuakeEntry> Data) { 
    Comparator<QuakeEntry> cmtr = new Comparator<QuakeEntry>() { 
     @Override 
     public int compare(QuakeEntry t, QuakeEntry t1) { 
      if (t.getMagnitude() < t1.getMagnitude()) 
       return -1; 
      if (t.getMagnitude() == t1.getMagnitude()) 
       return 1; 
      if (t1.getMagnitude() > t1.getMagnitude()) 
       return 0; 
     return 1; 
     } 
    }; 

    QuakeEntry max = Collections.max(Data, cmtr); 
    int maxIndex = Data.indexOf(max); 
    //---------------------------------------------------------------------- 
    System.out.println("//---------------------------------------------------"); 
    System.out.println("ArrayList sorted by Magnitude using inner class with Comparator"); 
    System.out.println("//---------------------------------------------------"); 
    Collections.sort(Data, cmtr); 
    for (QuakeEntry qe : Data) { 
     System.out.println(qe); 
    } 


    return maxIndex; 
} 

सभी वर्गों के लिए कोड: https://github.com/Evegen55/Java_DukeEdu_Coursera_2/blob/master/Earthquakes_Programming%20and%20Interfaces/src/earthquakes_programming_and_interfaces/QuakeEntry.java