This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
|
java:jfc_intf_collection [2021/04/05 07:17] gthanos [Διάτρεξη ενός Collection] |
java:jfc_intf_collection [2021/05/22 03:41] gthanos [2ος τρόπος - iterator] |
||
|---|---|---|---|
| Line 9: | Line 9: | ||
| - με τη βοήθεια ενός [[https:// | - με τη βοήθεια ενός [[https:// | ||
| - | ==== 1ος τρόπος - for() ==== | + | ==== 1ος τρόπος - For-Each Loop ==== |
| <code java StudentCollection.java> | <code java StudentCollection.java> | ||
| Line 16: | Line 16: | ||
| public class StudentCollection { | public class StudentCollection { | ||
| - | |||
| - | private Collection< | ||
| | | ||
| - | public | + | public |
| - | | + | |
| - | | + | |
| + | } | ||
| } | } | ||
| | | ||
| - | public | + | public |
| + | students = new LinkedList< | ||
| students.add(new Student(" | students.add(new Student(" | ||
| students.add(new Student(" | students.add(new Student(" | ||
| Line 30: | Line 30: | ||
| 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 47: | Line 37: | ||
| ==== 2ος τρόπος - iterator ==== | ==== 2ος τρόπος - iterator ==== | ||
| - | Μπορείτε να αντικαταστήσετε την '' | + | Μπορείτε να αντικαταστήσετε την '' |
| <code java> | <code java> | ||
| - | public void iterateList() { | + | public |
| Iterator< | Iterator< | ||
| while( it.hasNext() ) { | while( it.hasNext() ) { | ||