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 [2017/02/07 17:47] – [1ος τρόπος - for()] gthanos | java:jfc_intf_collection [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== Interface |
Το βασικό Interface στην Java το οποίο αποτελεί το ελάχιστον κοινό Interface των //Set//, //List//, //Queue//, // | Το βασικό Interface στην Java το οποίο αποτελεί το ελάχιστον κοινό Interface των //Set//, //List//, //Queue//, // | ||
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 75: | Line 70: | ||
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 89: | Line 78: | ||
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 < | Collection < | ||
sts.add(new Student(" | sts.add(new Student(" | ||
sts.add(new Student(" | sts.add(new Student(" | ||
- | */ | ||
- | if( stl.getStudents().containsAll(sts) ) { | + | if( students.containsAll(sts) ) { |
| | ||
} | } | ||
Line 116: | Line 94: | ||
</ | </ | ||
- | Στην συνέχεια | + | <WRAP tip center 80% round > |
- | + | Απαραίτητη προϋπόθεση για να δουλέψει σωστά η μέθοδος '' | |
- | <WRAP center 70% round tip> | + | |
- | Όταν συγκρίνονται | + | |
</ | </ | ||
Line 126: | 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 145: | 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 192: | 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.1486489643.txt.gz · Last modified: 2017/02/07 17:47 (external edit)