本迭代器用于课程的查询
package cn.hdeasy_01;
public interface ICourseIterator {
void next();
void previous();
boolean isFrist();
boolean isLast();
String getNextCourseName();
String getPreviousCourseName();
}
package cn.hdeasy_01;
public abstract class CourseList {
private String[]courseName;
public CourseList(String[]courseName){
this.courseName=courseName;
}
public String[] getCourseName(){
return courseName;
}
private class MyCourseIterator implements ICourseIterator {
private String[] coursorName;
private int fristIndex;
private int lastIndex;
public MyCourseIterator(CourseList list) {
this.coursorName=list.getCourseName();
fristIndex=-1;
lastIndex=coursorName.length;
}
@Override
public void next() {
if(fristIndex<coursorName.length){
fristIndex++;
}
}
public void previous() {
if(lastIndex>-1){
lastIndex--;
}
}
@Override
public boolean isFrist() {
return lastIndex==-1;
}
@Override
public boolean isLast() {
return fristIndex==coursorName.length;
}
@Override
public String getNextCourseName() {
return courseName[fristIndex];
}
@Override
public String getPreviousCourseName() {
return courseName[lastIndex];
}
}
public MyCourseIterator getIterator(){
return new MyCourseIterator(this);
};
}
package cn.hdeasy_01;
public class MyList extends CourseList {
public MyList(String[] courseName) {
super(courseName);
}
}
package cn.hdeasy_01;
public class IteratorDemos {
public static void main(String[] args) {
String[]cousers=new String[]{"语文","数学","英语","物理","化学"};
CourseList courseList;
ICourseIterator iterator;
MyList list=new MyList(cousers);
iterator=list.getIterator();
while(!iterator.isLast()){
iterator.next();
String nextCourseName = iterator.getNextCourseName();
System.out.println(nextCourseName);
}
while(!iterator.isFrist()){
iterator.previous();
String previousCourseName = iterator.getPreviousCourseName();
System.out.println(previousCourseName);
}
}
}