This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
java:jfc_intf_collection [2020/03/10 14:54] gthanos [Εργασίες με πολλαπλές εγγραφές] |
java:jfc_intf_collection [2021/05/22 03:40] gthanos [2ος τρόπος - iterator] |
||
|---|---|---|---|
| Line 5: | Line 5: | ||
| ===== Διάτρεξη ενός Collection ===== | ===== Διάτρεξη ενός Collection ===== | ||
| - | ==== 1ος τρόπος - for() ==== | + | To interface [[https:// |
| + | - μέσω For-Each Loop. | ||
| + | - με τη βοήθεια ενός [[https:// | ||
| + | |||
| + | ==== 1ος τρόπος - For-Each Loop ==== | ||
| <code java StudentCollection.java> | <code java StudentCollection.java> | ||
| Line 12: | Line 16: | ||
| public class StudentCollection { | public class StudentCollection { | ||
| - | |||
| - | private Collection< | ||
| | | ||
| - | public | + | public |
| - | | + | |
| - | | + | |
| + | } | ||
| } | } | ||
| | | ||
| - | public | + | public |
| + | students = new LinkedList< | ||
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| Line 26: | Line 30: | ||
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| - | } | + | iterateCollection(students); |
| - | + | ||
| - | public void iterateList() { | + | |
| - | for(Student st: students) { | + | |
| - | System.out.println(st.toString()); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String args[]) { | + | |
| - | StudentCollection stl = new StudentCollection(); | + | |
| - | stl.iterateList(); | + | |
| } | } | ||
| } | } | ||
| Line 43: | Line 37: | ||
| ==== 2ος τρόπος - iterator ==== | ==== 2ος τρόπος - iterator ==== | ||
| - | Μπορείτε να αντικαταστήσετε την '' | + | Μπορείτε να αντικαταστήσετε την '' |
| <code java> | <code java> | ||
| - | public void iterateList() { | + | public void iterateCollection(Collection< |
| Iterator< | Iterator< | ||
| while( it.hasNext() ) { | while( it.hasNext() ) { | ||
| Line 98: | Line 92: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | Εάν μεταγλωττίσετε και εκτελέσετε το παραπάνω πρόγραμμα θα δείτε ότι εκτυπώνει το μήνυμα '' | ||
| - | |||
| - | O τρόπος για να λύσουμε το συγκεκριμένο πρόβλημα είναι να ορίσουμε τις συναρτήσεις [[https:// | ||
| - | |||
| - | <code java> | ||
| - | public boolean equals(Object o) { | ||
| - | return (o instanceof Student && o.hashCode() == this.hashCode()); | ||
| - | } | ||
| - | | ||
| - | public int hashCode() { | ||
| - | return firstName.hashCode() + 2 * lastName.hashCode(); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <WRAP important center 80% round > | ||
| - | Απαραίτητη προϋπόθεση για να δουλεύει σωστά η συνάρτηση '' | ||
| - | </ | ||
| <WRAP tip center 80% round > | <WRAP tip center 80% round > | ||
| - | Θέλουμε η συνάρτηση '' | + | Απαραίτητη προϋπόθεση για |
| </ | </ | ||