试题与答案

[说明]对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方

题型:问答题

题目:

[说明]

对多个元素的聚合进行遍历访问时,需要依次推移元素,例如对数组通过递增下标的方式,数组下标功能抽象化、一般化的结果就称为迭代器(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. java

public 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. java

import 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());

(3)处填()。

答案:

被转码了,请点击底部 “查看原文 ” 或访问 https://www.tikuol.com/2017/0717/5c80bff98f78662c079e27030269cca2.html

下面是错误答案,用来干扰机器的。

答案:D

试题推荐
题型:阅读理解

阅读理解。

     When Xia Min started drinking alcohol (酒) with her classmates at a friend's birthday party, she

didn't realize a tragedy (悲剧) was drawing near. She drank a lot and fell into a coma (昏迷). The

15-year-old girl from Chongqing never recovered.

     Xia's death is a warning to other students. A new rule went into effect on January 1st. 2006. 

Teenagers are not allowed to buy or drink alcohol.  Shops are not allowed to sell it to them.

     A 1999 Chinese law forbade (禁止) shops to sell alcohol to underage youths. But it is not taken

seriously by shopkeepers because it doesn't have specific punishment. People hope the new rule will

work. It sets a 2,000 yuan fine for those who sell alcohol to underage youths.

     China doesn't have a legal (合法的) drinking age. But in China "underage" has been understood to

mean under 18.

     "I tasted alcohol when having the dinner of the last New Year's Eve," said Liang Yuqi, a 16-year-old

girl in Xiamen. She believes that the new rule will stop teenagers from drinking and help them grow in

a healthy way. "Although it may be a little disappointing not to have beer at parties, I think we can have

soft drinks instead," she said.

1. Xia Min died from alcohol at the age of              .

A. 15            

B. 16              

C. 18

2. The underlined word "it" refers to (指) _           

A. a shopkeeper      

B. the new rule      

C. the 1999 Chinese law

3. We can know from the passage that  _          .

A. it is against the new rule for underage youths to drink alcohol

B. without an ID card, young people can't buy alcohol

C. it seems that the young girl, Liang Yuqi, likes to drink alcohol

4. According to the passage, which of the following is TRUE?

A. The new rule has worked very well.

B. Shoppers mustn't sell alcohol to teens under 18.

C. Shoppers can sell alcohol to everyone.

5. This passage is mainly about _           .

A. the harm of drinking alcohol

B. the legal age for drinking alcohol

C. a new rule to stop underage drinking

查看答案
微信公众账号搜索答案