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 [2015/03/22 19:34] gthanos |
java:jfc_intf_collection [2021/05/22 02:47] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Collection Interface ====== | ||
| - | |||
| - | Το βασικό Interface στην Java το οποίο αποτελεί το ελάχιστον κοινό Interface των //Set//, //List//, //Queue//, // | ||
| - | |||
| - | ===== Διάτρεξη ενός Collection ===== | ||
| - | |||
| - | ==== 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 { | ||
| - | |||
| - | 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 Collection< | ||
| - | return students; | ||
| - | } | ||
| - | | ||
| - | public static void main(String args[]) { | ||
| - | StudentCollection stl = new StudentCollection(); | ||
| - | Collection < | ||
| - | | ||
| - | /* | ||
| - | Collection < | ||
| - | sts.add(new Student(" | ||
| - | sts.add(new Student(" | ||
| - | */ | ||
| - | |||
| - | if( stl.getStudents().containsAll(sts) ) { | ||
| - | | ||
| - | } | ||
| - | else { | ||
| - | | ||
| - | } | ||
| - | | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Στην συνέχεια βάλτε σχόλια στην γραμμή που υποδεικνύουν τα σχόλια και αφαιρέστε το τμήμα του κώδικα που είναι σε σχόλια. Μεταγλωττίστε και τρέξτε. Ποιό είναι το αποτέλεσμα αυτή την φορά; Γιατί συμβαίνει αυτό; | ||
| - | |||
| - | <WRAP center 70% round tip> | ||
| - | Όταν συγκρίνονται δύο // | ||
| - | </ | ||
| - | |||
| - | * **[[https:// | ||
| - | |||
| - | <code java StudentCollection.java> | ||
| - | import java.util.*; | ||
| - | import java.lang.*; | ||
| - | 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 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 < | ||
| - | sts.add(new Student(" | ||
| - | sts.add(new Student(" | ||
| - | | ||
| - | stl.getStudents().addAll(sts); | ||
| - | stl.iterateList(); | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | * **[[https:// | ||
| - | <code java StudentCollection.java> | ||
| - | import java.util.*; | ||
| - | import java.lang.*; | ||
| - | |||
| - | 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 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 < | ||
| - | | ||
| - | //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(" | ||
| - | */ | ||
| - | | ||
| - | stl.getStudents().removeAll(sts); | ||
| - | stl.iterateList(); | ||
| - | | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <WRAP center 70% round tip> | ||
| - | Και εδώ ισχύει ότι αναφέραμε παραπάνω αναφορικά με την σύγκριση των // | ||
| - | </ | ||
| - | |||
| - | |||