java:jfc_intf_collection
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:jfc_intf_collection [2020/03/10 14:54] – [Εργασίες με πολλαπλές εγγραφές] gthanos | java:jfc_intf_collection [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| 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 |
| + | Collection< | ||
| + | students = new LinkedList< | ||
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| Line 26: | Line 31: | ||
| 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 38: | ||
| ==== 2ος τρόπος - iterator ==== | ==== 2ος τρόπος - iterator ==== | ||
| - | Μπορείτε να αντικαταστήσετε την '' | + | Μπορείτε να αντικαταστήσετε την '' |
| <code java> | <code java> | ||
| - | public void iterateList() { | + | public |
| Iterator< | Iterator< | ||
| while( it.hasNext() ) { | while( it.hasNext() ) { | ||
| Line 98: | Line 93: | ||
| } | } | ||
| </ | </ | ||
| - | |||
| - | Εάν μεταγλωττίσετε και εκτελέσετε το παραπάνω πρόγραμμα θα δείτε ότι εκτυπώνει το μήνυμα '' | ||
| - | |||
| - | 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 > | ||
| - | Θέλουμε η συνάρτηση '' | + | Απαραίτητη προϋπόθεση για |
| </ | </ | ||
java/jfc_intf_collection.1583852079.txt.gz · Last modified: 2020/03/10 14:54 (external edit)
