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:31] – [Εργασίες με πολλαπλές εγγραφές] 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 99: | Line 94: | ||
| </ | </ | ||
| - | Εάν μεταγλωττίσετε και εκτελέσετε το παραπάνω πρόγραμμα θα δείτε ότι εκτυπώνει το μήνυμα | + | <WRAP tip center 80% round > |
| - | + | Απαραίτητη προϋπόθεση | |
| - | O τρόπος για να λύσουμε το συγκεκριμένο πρόβλημα είναι να ορίσουμε ξανά τις συναρτήσεις [[https:// | + | |
| - | + | ||
| - | <WRAP important center 80% round > | + | |
| - | Απαραίτητη προϋπόθεση για να δουλεύει σωστά η συνάρτηση '' | + | |
| </ | </ | ||
| Line 111: | Line 102: | ||
| <code java StudentCollection.java> | <code java StudentCollection.java> | ||
| import java.util.*; | import java.util.*; | ||
| - | import java.lang.*; | ||
| - | import java.util.*; | ||
| - | import java.lang.*; | ||
| public class StudentCollection { | public class StudentCollection { | ||
| - | + | ||
| - | private Collection< | + | public |
| - | | + | |
| - | public | + | |
| - | students = new LinkedList<Student>(); | + | |
| - | populateList(); | + | |
| - | } | + | |
| - | + | ||
| - | public final void populateList() { | + | |
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| Line 130: | Line 112: | ||
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| - | } | + | |
| - | + | Collection < | |
| - | public | + | |
| - | | + | |
| - | } | + | |
| - | public void iterateList() { | + | students.addAll(sts); |
| - | | + | |
| - | System.out.println(st.toString()); | + | |
| - | | + | |
| } | } | ||
| | | ||
| - | public static void main(String args[]) { | + | public static void print(Collection< |
| - | | + | |
| - | Collection | + | |
| - | | + | |
| - | sts.add(new Student(" | + | |
| - | + | ||
| - | stl.getStudents().addAll(sts); | + | |
| - | stl.iterateList(); | + | |
| } | } | ||
| - | } | + | } |
| </ | </ | ||
| + | |||
| + | <WRAP todo center 80% round > | ||
| + | Στον παραπάνω κώδικα αντικαταστήστε την πρώτη γραμμή της '' | ||
| + | < | ||
| + | Collection< | ||
| + | </ | ||
| + | |||
| + | Τι παρατηρείτε; | ||
| + | </ | ||
| * **[[https:// | * **[[https:// | ||
| <code java StudentCollection.java> | <code java StudentCollection.java> | ||
| import java.util.*; | import java.util.*; | ||
| - | import java.lang.*; | ||
| - | |||
| - | import java.util.*; | ||
| - | import java.lang.*; | ||
| public class StudentCollection { | public class StudentCollection { | ||
| - | + | ||
| - | private Collection< | + | public |
| - | | + | |
| - | public | + | |
| - | students = new LinkedList<Student>(); | + | |
| - | populateList(); | + | |
| - | } | + | |
| - | + | ||
| - | public final void populateList() { | + | |
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| Line 177: | Line 152: | ||
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| - | } | + | |
| - | + | ||
| - | public Collection< | + | |
| - | return students; | + | |
| - | } | + | |
| - | + | ||
| - | public void iterateList() { | + | |
| - | for(Student st: students) { | + | |
| - | System.out.println(st.toString()); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | public static void main(String args[]) { | + | |
| - | StudentCollection stl = new StudentCollection(); | + | |
| Collection < | Collection < | ||
| - | | ||
| - | //comment for statement | ||
| - | int i=0; | ||
| - | for(Student st : stl.getStudents()) { | ||
| - | sts.add(st); | ||
| - | if(++i==2) break; | ||
| - | } | ||
| - | | ||
| - | //and uncomment this code below | ||
| - | /* | ||
| sts.add(new Student(" | sts.add(new Student(" | ||
| sts.add(new Student(" | sts.add(new Student(" | ||
| - | */ | + | |
| - | | + | |
| - | stl.getStudents().removeAll(sts); | + | students.removeAll(sts); |
| - | | + | |
| | | ||
| } | } | ||
| - | } | + | |
| + | public static void print(Collection< | ||
| + | Iterator< | ||
| + | while(it.hasNext()) | ||
| + | System.out.println(it.next()); | ||
| + | } | ||
| + | } | ||
| </ | </ | ||
| - | <WRAP center 70% round tip> | + | Παρατηρήστε ότι |
| - | Και εδώ ισχύει ότι | + | |
| - | </ | + | |
| |Προηγούμενο: | |Προηγούμενο: | ||
java/jfc_intf_collection.1583850660.txt.gz · Last modified: 2020/03/10 14:31 by gthanos
