This shows you the differences between two versions of the page.
|
java:comparable [2020/02/25 08:17] gthanos |
java:comparable [2020/03/15 18:30] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Συγκρίνοντας αντικείμενα μεταξύ τους ====== | ||
| - | |||
| - | ===== Το interface java.lang.Comparable ===== | ||
| - | |||
| - | Συχνά προκύπτει η ανάγκη να ταξινομήσουμε αντικείμενα ή να εφαρμόσουμε δυαδική αναζήτηση πάνω σε ένα ήδη ταξινομημένο σύνολο. Προκειμένου να εφαρμόσουμε τους παραπάνω αλγορίθμους είναι αναγκαίο να μπορούμε να συγκρίνουμε αντικείμενα μεταξύ τους. Ας υποθέσουμε ότι έχουμε μια σειρά από αντικείμενα της κλάσης '' | ||
| - | |||
| - | <code java Rectangle.java> | ||
| - | class Rectangle { | ||
| - | | ||
| - | int width; | ||
| - | int height; | ||
| - | |||
| - | public Rectangle(int initWidth, int initHeight) { | ||
| - | width = initWidth; | ||
| - | height = initHeight; | ||
| - | } | ||
| - | | ||
| - | int area() { | ||
| - | return width * height; | ||
| - | } | ||
| - | public String toString() { | ||
| - | return " | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code java SortRectangleArray.java> | ||
| - | import java.util.Random; | ||
| - | import java.util.Arrays; | ||
| - | |||
| - | class SortRectangleArray { | ||
| - | | ||
| - | public static void main(String []args) { | ||
| - | Random rand = new Random(12345); | ||
| - | Rectangle []rectangles = new Rectangle[5]; | ||
| - | for(int i=0;i<5; i++) | ||
| - | rectangles[i] = new Rectangle(rand.nextInt(10), | ||
| - | | ||
| - | print_rectangles(rectangles); | ||
| - | Arrays.sort(rectangles); | ||
| - | print_rectangles(rectangles); | ||
| - | } | ||
| - | | ||
| - | public static void print_rectangles(Rectangle []rectangles) { | ||
| - | System.out.println(" | ||
| - | for(int i=0; i< | ||
| - | System.out.println(i+" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | O παραπάνω κώδικας αν και μεταγλωττίζεται τερματίζει αναπάντεχα με ένα μήνυμα της μορφής: | ||
| - | < | ||
| - | Exception in thread " | ||
| - | at java.base/ | ||
| - | at java.base/ | ||
| - | at java.base/ | ||
| - | at SortRectangleArray.main(SortRectangleArray.java: | ||
| - | </ | ||
| - | |||
| - | Ο λόγος που συμβαίνει αυτό είναι ότι δεν υπάρχει τρόπος να συγκριθούν τα αντικείμενα τύπου '' | ||
| - | * **>0** έαν το τρέχον αντικείμενο είναι μεγαλύτερο από το '' | ||
| - | * **<0** εάν το τρέχον αντικείμενο είναι μικρότερο από το '' | ||
| - | * **==0** εάν το τρέχον αντικείμενο είναι ίσο με το '' | ||
| - | |||
| - | Το κριτήριο μέσω του οποίου θα συγκρίνετε δύο αντικείμενα τύπου '' | ||
| - | |||
| - | <code java> | ||
| - | public int compareTo(Rectangle r) { | ||
| - | return this.area() - r.area(); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Επίσης, | ||
| - | |||
| - | <code java Rectangle.java> | ||
| - | class Rectangle implements java.lang.Comparable< | ||
| - | | ||
| - | int width; | ||
| - | int height; | ||
| - | |||
| - | public Rectangle(int initWidth, int initHeight) { | ||
| - | width = initWidth; | ||
| - | height = initHeight; | ||
| - | } | ||
| - | | ||
| - | int area() { | ||
| - | return width * height; | ||
| - | } | ||
| - | public String toString() { | ||
| - | return " | ||
| - | } | ||
| - | | ||
| - | public int compareTo(Rectangle r) { | ||
| - | return this.area() - r.area(); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <WRAP tip 80% center round> | ||
| - | Οι βασικές κλάσεις του πακέτου java.lang (String, | ||
| - | </ | ||
| - | |||
| - | ===== Το interface java.util.Comparator ===== | ||
| - | |||
| - | Στο προηγούμενο παράδειγμα, | ||