网友您好, 请在下方输入框内输入要搜索的题目:
题目内容
(请给出正确答案)
使用Iterator时,判断是否存在下一个元素可以使用以下哪个方法( )。
A、next()
B、hash()
C、hasPrevious()
D、hasNext()
参考答案
更多 “ 使用Iterator时,判断是否存在下一个元素可以使用以下哪个方法( )。 A、next()B、hash()C、hasPrevious()D、hasNext() ” 相关考题
考题
阅读以下说明和Java代码,回答问题[说明]对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图6-1显示了各个类间的关系。以下是JAVA语言实现,能够正确编译通过。[图6-1][Java代码]//Iterator. java文件public interface Iterator {public abstract boolean hasNext();public abstract Object next();}//Aggregate. java文件public interface Aggregate {public abstract Iterator iterator();}//Book. javapublic class Book {//省略具体方法和属性}//BookshelfIterator. java文件public class Bookshelf工terator (1) Iterator{private BookShelf bookShelf;private int index;public BookshelfIterator(BookShelf bookShelf) {this. bookShelf = bookShelf;this. index = 0;}public boolean hasNext(){//判断是否还有下一个元素if(index bookShelf. getLength()){return true;}else{return false;}}public Object next()f//取得下一个元素Book book = bookShelf. getBookAt(index);index++;return book;}}//BookShelf. javaimport java. util. Vector;public class BookShelf {private Vector books;public BookShelf(int initialsize){this. books = new Vector(initialsize);}public Book getBookAt(int index){return(Book)books.get(index);}public int getLength(){return books.size();}public Iterator iterator(){return new BookShelfIterator( (2) );}}//Main. java文件public class Main {public static void main(String args){BookShelf bookShelf = new BookShelf(4);//将书籍上架,省略代码Iterator it = bookShelf. (3) ;while( (4) ){//遍历书架,输出书名Book book = (Book)it. (5) ;System.out.printin(" "+book.getName());}}}
考题
阅读下列函数说明和C++代码,回答问题[说明]对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(Iterator)。模式以下程序模拟将书籍(Book)放到书架(BookShelf)上并依次输出书名。这样就要涉及到遍历整个书架的过程。使用迭代器Iterator实现。图5-1显示了各个类间的关系。以下是C++语言实现,能够正确编译通过。[图5-1][C++代码]template (1) class Iterator{public:virtual bool hasNext() = 0;(2) Object* next() = 0;};class Book{//省略具体方法和属性};class BookShelf{private:vector books;public:BookShelf(){}Book* getBookAt(int index){return booksindex;}int getLength(){return books. size();}};templateclass BookshelfIterator : public (3) {private:BookShelf * bookShelf;int index;public:BookshelfIterator(BookShelf *bookShelf){this-bookShelf = bookShelf;index = 0;}bool hasNext(){//判断是否还有下一个元素if(index bookShelf-getLength()){return true;}else{return false;}}Objeot* next(){//取得下一个元素return bookShelf-getBookAt(index++);}};int main(){BookShelf bookShelf;//将书籍上架,省略代码Book *book;Iterator *it = new BookShelfIterator( (4) );while( (5) ){//遍历书架,输出书名book=(Book*)it-next();/*访问元素*/}return 0;}
考题
创建学生类,包含学号、姓名、综合测评总分,使用ArrayList保存学生对象。先对学生对象按综合测评总分升序排列,再使用Iterator的next方法输出List中的元素。再按学生的学号从小到大排序,使用Iterator的forEachRemaining方法输出
考题
使用Iterator遍历集合时,首先需要调用 方法判断是否存在下一个元素,若存在下一个元素,则调用 方法取出该元素。
热门标签
最新试卷