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:14] – [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface] gthanos | java:anon_inner_classes [2023/03/23 20:43] (current) – [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface] gthanos | ||
---|---|---|---|
Line 12: | Line 12: | ||
===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface ===== | ===== Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως υλοποίηση ενός interface ===== | ||
- | Στο παρακάτω παράδειγμα ορίζουμε την κλάση **Student** η οποία έχει τα τρία (3) πεδία **firstName**, | + | Στο παρακάτω παράδειγμα ορίζουμε την κλάση **Student** η οποία έχει τα τρία (3) πεδία **firstName**, |
- | + | ||
- | <code txt students.txt> | + | |
- | 4596 Super Man | + | |
- | 5819 Peter Pan | + | |
- | 1694 Susan Brown | + | |
- | 7895 Lena Smith | + | |
- | 3256 Oscar Gonzales | + | |
- | 1243 Edgar Wallace | + | |
- | 3265 Kate Gordon | + | |
- | 4377 Leda Evans | + | |
- | 1118 David Lerroy | + | |
- | 8744 Amelia Dray | + | |
- | </ | + | |
<code java Student.java> | <code java Student.java> | ||
Line 67: | 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 76: | Line 63: | ||
} | } | ||
); | ); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Στη μέθοδο **// | ||
+ | * με τη σειρά που τα διαβάσαμε | ||
+ | * με λεξικογραφική σειρά. | ||
+ | * με βάση το ΑΕΜ | ||
+ | |||
+ | <code txt students.txt> | ||
+ | 4596 Super Man | ||
+ | 5819 Peter Pan | ||
+ | 1694 Susan Brown | ||
+ | 7895 Lena Smith | ||
+ | 3256 Oscar Gonzales | ||
+ | 1243 Edgar Wallace | ||
+ | 3265 Kate Gordon | ||
+ | 4377 Leda Evans | ||
+ | 1118 David Lerroy | ||
+ | 8744 Amelia Dray | ||
+ | </ | ||
+ | |||
+ | <code java SortListOfStudents.java> | ||
+ | import java.io.*; | ||
+ | import java.util.*; | ||
+ | |||
+ | public class SortListOfStudents { | ||
+ | | ||
+ | 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 static String toString(List< | ||
+ | StringBuilder str = new StringBuilder(); | ||
+ | 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)); | ||
+ | Student.sortLexicographically(students); | ||
+ | System.out.println(" | ||
+ | System.out.println(toString(students)); | ||
+ | Student.sortByAEM(students); | ||
+ | System.out.println(" | ||
+ | System.out.println(toString(students)); | ||
} | } | ||
} | } | ||
Line 105: | 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 119: | 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 152: | 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.1618208098.txt.gz · Last modified: 2021/04/12 05:14 (external edit)