This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
java:jfc_intf_collection [2021/04/05 07:17] gthanos [Διάτρεξη ενός Collection] |
java:jfc_intf_collection [2021/05/22 02:47] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Interface java.util.Collection | ||
| - | |||
| - | Το βασικό Interface στην Java το οποίο αποτελεί το ελάχιστον κοινό Interface των //Set//, //List//, //Queue//, // | ||
| - | |||
| - | ===== Διάτρεξη ενός Collection ===== | ||
| - | |||
| - | To interface [[https:// | ||
| - | - μέσω For-Each Loop. | ||
| - | - με τη βοήθεια ενός [[https:// | ||
| - | |||
| - | ==== 1ος τρόπος - for() ==== | ||
| - | |||
| - | <code java StudentCollection.java> | ||
| - | import java.util.*; | ||
| - | import java.lang.*; | ||
| - | |||
| - | public class StudentCollection { | ||
| - | |||
| - | private Collection< | ||
| - | | ||
| - | public StudentCollection() { | ||
| - | students = new LinkedList< | ||
| - | populateList(); | ||
| - | } | ||
| - | | ||
| - | public final void populateList() { | ||
| - | students.add(new Student(" | ||
| - | students.add(new Student(" | ||
| - | students.add(new Student(" | ||
| - | students.add(new Student(" | ||
| - | students.add(new Student(" | ||
| - | } | ||
| - | | ||
| - | public void iterateList() { | ||
| - | for(Student st: students) { | ||
| - | System.out.println(st.toString()); | ||
| - | } | ||
| - | } | ||
| - | | ||
| - | public static void main(String args[]) { | ||
| - | StudentCollection stl = new StudentCollection(); | ||
| - | stl.iterateList(); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ==== 2ος τρόπος - iterator ==== | ||
| - | |||
| - | Μπορείτε να αντικαταστήσετε την '' | ||
| - | <code java> | ||
| - | public void iterateList() { | ||
| - | 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()); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Παρατηρήστε ότι η μέθοδος '' | ||
| - | |||
| - | |Προηγούμενο: | ||