This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Previous revision Next revision Both sides next revision | ||
|
java:jfc_intf_collection [2021/05/22 02:47] |
java:jfc_intf_collection [2021/05/22 03:41] gthanos [2ος τρόπος - iterator] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Interface java.util.Collection | ||
| + | |||
| + | Το βασικό Interface στην Java το οποίο αποτελεί το ελάχιστον κοινό Interface των //Set//, //List//, //Queue//, // | ||
| + | |||
| + | ===== Διάτρεξη ενός Collection ===== | ||
| + | |||
| + | To interface [[https:// | ||
| + | - μέσω For-Each Loop. | ||
| + | - με τη βοήθεια ενός [[https:// | ||
| + | |||
| + | ==== 1ος τρόπος - For-Each Loop ==== | ||
| + | |||
| + | <code java StudentCollection.java> | ||
| + | import java.util.*; | ||
| + | import java.lang.*; | ||
| + | |||
| + | public class StudentCollection { | ||
| + | | ||
| + | public static void iterateCollection(Collection< | ||
| + | for(Student st: students) { | ||
| + | System.out.println(st.toString()); | ||
| + | } | ||
| + | } | ||
| + | | ||
| + | public static void main(String args[]) { | ||
| + | students = new LinkedList< | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | iterateCollection(students); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== 2ος τρόπος - iterator ==== | ||
| + | |||
| + | Μπορείτε να αντικαταστήσετε την '' | ||
| + | <code java> | ||
| + | public static void iterateCollection(Collection< | ||
| + | Iterator< | ||
| + | while( it.hasNext() ) { | ||
| + | System.out.println(it.next().toString()); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Συνιστάται ο 2ος τρόπος αντί για τον 1ο εάν θέλετε καθώς διατρέχετε τη συλλογή να διαγράφετε και εγγραφές. | ||
| + | |||
| + | ===== Βασικές μέθοδοι ===== | ||
| + | |||
| + | * **[[https:// | ||
| + | * **[[https:// | ||
| + | * **[[https:// | ||
| + | |||
| + | |||
| + | ===== Εργασίες με πολλαπλές εγγραφές ===== | ||
| + | |||
| + | * **[[https:// | ||
| + | * **[[https:// | ||
| + | |||
| + | <code java StudentCollection.java> | ||
| + | import java.util.*; | ||
| + | import java.lang.*; | ||
| + | |||
| + | import java.util.*; | ||
| + | import java.lang.*; | ||
| + | |||
| + | public class StudentCollection { | ||
| + | |||
| + | public static void main(String args[]) { | ||
| + | Collection< | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | | ||
| + | Collection < | ||
| + | sts.add(new Student(" | ||
| + | sts.add(new Student(" | ||
| + | |||
| + | if( students.containsAll(sts) ) { | ||
| + | | ||
| + | } | ||
| + | else { | ||
| + | | ||
| + | } | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP tip center 80% round > | ||
| + | Απαραίτητη προϋπόθεση για να δουλέψει σωστά η μέθοδος '' | ||
| + | </ | ||
| + | |||
| + | * **[[https:// | ||
| + | |||
| + | <code java StudentCollection.java> | ||
| + | import java.util.*; | ||
| + | |||
| + | public class StudentCollection { | ||
| + | |||
| + | public static void main(String args[]) { | ||
| + | Collection< | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | | ||
| + | Collection < | ||
| + | sts.add(new Student(" | ||
| + | sts.add(new Student(" | ||
| + | |||
| + | students.addAll(sts); | ||
| + | print(students); | ||
| + | | ||
| + | } | ||
| + | | ||
| + | public static void print(Collection< | ||
| + | Iterator< | ||
| + | while(it.hasNext()) | ||
| + | System.out.println(it.next()); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <WRAP todo center 80% round > | ||
| + | Στον παραπάνω κώδικα αντικαταστήστε την πρώτη γραμμή της '' | ||
| + | < | ||
| + | Collection< | ||
| + | </ | ||
| + | |||
| + | Τι παρατηρείτε; | ||
| + | </ | ||
| + | |||
| + | * **[[https:// | ||
| + | <code java StudentCollection.java> | ||
| + | import java.util.*; | ||
| + | |||
| + | public class StudentCollection { | ||
| + | |||
| + | public static void main(String args[]) { | ||
| + | Collection< | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | students.add(new Student(" | ||
| + | | ||
| + | Collection < | ||
| + | sts.add(new Student(" | ||
| + | sts.add(new Student(" | ||
| + | |||
| + | students.addAll(sts); | ||
| + | students.removeAll(sts); | ||
| + | print(students); | ||
| + | | ||
| + | } | ||
| + | | ||
| + | public static void print(Collection< | ||
| + | Iterator< | ||
| + | while(it.hasNext()) | ||
| + | System.out.println(it.next()); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Παρατηρήστε ότι η μέθοδος '' | ||
| + | |||
| + | |Προηγούμενο: | ||