User Tools

Site Tools


java:anon_inner_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
java:anon_inner_classes [2021/04/12 06:21]
gthanos
java:anon_inner_classes [2021/04/12 06:23]
gthanos [Παράδειγμα - Ανώνυμη εμφωλευμένη κλάση ως επέκταση υφιστάμενης κλάσης]
Line 67: Line 67:
 </code> </code>
  
-Στη μέθοδο **//main//** διαβάζουμε από το αρχείο students.txt και καταχωρούμε το περιεχόμενο του σε μία λίστα (ArrayList<Student>). Στη συνέχεια εκτυπώνουμε τα στοιχεία της λίστας+Στη μέθοδο **//main//** διαβάζουμε από το αρχείο **students.txt** και καταχωρούμε το περιεχόμενο του σε μία λίστα (**ArrayList<Student>**). Στη συνέχεια εκτυπώνουμε τα στοιχεία της λίστας
   * με τη σειρά που τα διαβάσαμε   * με τη σειρά που τα διαβάσαμε
   * με λεξικογραφική σειρά.   * με λεξικογραφική σειρά.
Line 159: Line 159:
   }   }
      
-  abstract class BasicComparator implements Comparator<Student> { +  static abstract class BasicComparator implements Comparator<Student> {
-   +
   }   }
          
-  Comparator<Student> lexicographicCmp = new BasicComparator() {+  static Comparator<Student> lexicographicCmp = new BasicComparator() {
     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<Student> students, File f) {     
-    try(Scanner sc = new Scanner(f)) { 
-      while(sc.hasNextLine()) {         
-        students.add( new Student(sc.nextInt(), sc.next(), sc.next()) ); 
-      } 
-    } 
-    catch(FileNotFoundException ex) { 
-      System.out.println("File \""+f.getName()+"\" was not found!"); 
-    } 
-    catch(InputMismatchException ex) { 
-      System.out.println("Input mismatch while reading with scanner!!!"); 
-      ex.printStackTrace(); 
-      System.exit(-1); 
-    } 
-  } 
      
-  public void sortLexicographically(List<Student> students) {    +  public static void sortLexicographically(List<Student> students) {    
     Collections.sort(students, lexicographicCmp);     Collections.sort(students, lexicographicCmp);
   }   }
          
-  public void sortByAEM(List<Student> students) {+  public static void sortByAEM(List<Student> students) {
     Collections.sort(students, new BasicComparator() {     Collections.sort(students, new BasicComparator() {
         public int compare(Student st1, Student st2) {         public int compare(Student st1, Student st2) {
Line 206: Line 189:
       }       }
     );     );
-  } +  } 
-   +
-  public static String toString(List<Student> students) { +
-    StringBuffer str = new StringBuffer(); +
-    for(Student st : students)  +
-      str.append(st+"\n"); +
-    str.append("---------------------------------\n"); +
-    return str.toString(); +
-  } +
-   +
-  public static void main(String []args) { +
-    if(args.length == 0) { +
-      System.out.println("Insufficient number of arguments!\n"); +
-      return; +
-    } +
-    List<Student> students = new ArrayList<Student>(); +
-    readFromFile(students, new File(args[0])); +
-    System.out.println(" --- Init Ordering ---\n"); +
-    System.out.println(toString(students)); +
-    students.get(0).sortLexicographically(students); +
-    System.out.println(" --- Lexicographic Ordering ---\n"); +
-    System.out.println(toString(students)); +
-    students.get(0).sortByAEM(students); +
-    System.out.println(" --- AEM Ordering ---\n"); +
-    System.out.println(toString(students)); +
-  } +
-    +
 } }
 </code> </code>
java/anon_inner_classes.txt · Last modified: 2023/03/23 20:43 by gthanos