java:anon_inner_classes
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:anon_inner_classes [2021/04/12 06:21] – gthanos | java:anon_inner_classes [2023/03/23 20:43] (current) – [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface] gthanos | ||
|---|---|---|---|
| Line 54: | Line 54: | ||
| Collections.sort(students, | Collections.sort(students, | ||
| public int compare(Student st1, Student st2) { | public int compare(Student st1, Student st2) { | ||
| - | int cmp = cmp = st1.AEM - st2.AEM; | + | int cmp = st1.AEM - st2.AEM; |
| if(cmp==0) | if(cmp==0) | ||
| cmp = st1.lastName.compareTo(st2.lastName); | cmp = st1.lastName.compareTo(st2.lastName); | ||
| Line 67: | Line 67: | ||
| </ | </ | ||
| - | Στη μέθοδο **// | + | Στη μέθοδο **// |
| * με τη σειρά που τα διαβάσαμε | * με τη σειρά που τα διαβάσαμε | ||
| * με λεξικογραφική σειρά. | * με λεξικογραφική σειρά. | ||
| Line 85: | Line 85: | ||
| </ | </ | ||
| - | <code java> | + | < |
| import java.io.*; | import java.io.*; | ||
| import java.util.*; | import java.util.*; | ||
| Line 159: | Line 159: | ||
| } | } | ||
| | | ||
| - | abstract class BasicComparator implements Comparator< | + | |
| - | + | ||
| } | } | ||
| | | ||
| - | Comparator< | + | |
| public int compare(Student st1, Student st2) { | public int compare(Student st1, Student st2) { | ||
| int cmp = st1.lastName.compareTo(st2.lastName); | int cmp = st1.lastName.compareTo(st2.lastName); | ||
| Line 173: | Line 172: | ||
| } | } | ||
| }; | }; | ||
| - | |||
| - | public static void readFromFile(List< | ||
| - | try(Scanner sc = new Scanner(f)) { | ||
| - | while(sc.hasNextLine()) { | ||
| - | students.add( new Student(sc.nextInt(), | ||
| - | } | ||
| - | } | ||
| - | catch(FileNotFoundException ex) { | ||
| - | System.out.println(" | ||
| - | } | ||
| - | catch(InputMismatchException ex) { | ||
| - | System.out.println(" | ||
| - | ex.printStackTrace(); | ||
| - | System.exit(-1); | ||
| - | } | ||
| - | } | ||
| | | ||
| - | public void sortLexicographically(List< | + | public |
| Collections.sort(students, | Collections.sort(students, | ||
| } | } | ||
| | | ||
| - | public void sortByAEM(List< | + | public |
| Collections.sort(students, | Collections.sort(students, | ||
| public int compare(Student st1, Student st2) { | public int compare(Student st1, Student st2) { | ||
| Line 206: | Line 189: | ||
| } | } | ||
| ); | ); | ||
| - | } | + | } |
| - | + | ||
| - | public static String toString(List< | + | |
| - | StringBuffer str = new StringBuffer(); | + | |
| - | for(Student st : students) | + | |
| - | str.append(st+" | + | |
| - | str.append(" | + | |
| - | return str.toString(); | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String []args) { | + | |
| - | if(args.length == 0) { | + | |
| - | System.out.println(" | + | |
| - | return; | + | |
| - | } | + | |
| - | List< | + | |
| - | readFromFile(students, | + | |
| - | System.out.println(" | + | |
| - | System.out.println(toString(students)); | + | |
| - | students.get(0).sortLexicographically(students); | + | |
| - | System.out.println(" | + | |
| - | System.out.println(toString(students)); | + | |
| - | students.get(0).sortByAEM(students); | + | |
| - | System.out.println(" | + | |
| - | System.out.println(toString(students)); | + | |
| - | } | + | |
| - | | + | |
| } | } | ||
| </ | </ | ||
java/anon_inner_classes.1618208462.txt.gz · Last modified: 2021/04/12 05:21 (external edit)
