代码其它写法的详细Iterator it = bookShelf.iterator(); while (it.hasNext()) { Book book = (Book)it.next(); System.out.println(book.getName());
时间: 2023-05-18 15:04:03 浏览: 55
这段代码使用了迭代器模式来遍历书架(bookShelf)中的书籍(Book)并输出它们的名称。如果您想要了解其它写法,可以考虑使用增强for循环或者Java 8中的Stream API来实现相同的功能。例如,使用增强for循环可以这样写:
for (Book book : bookShelf) {
System.out.println(book.getName());
}
而使用Stream API可以这样写:
bookShelf.stream().map(Book::getName).forEach(System.out::println);
相关问题
代码的其它写法详细public class Main { public static void main(String[] args) { BookShelf bookShelf = new BookShelf(4); bookShelf.appendBook(new Book("Around the World in 80 Days")); bookShelf.appendBook(new Book("Bible")); bookShelf.appendBook(new Book("Cinderella")); bookShelf.appendBook(new Book("Daddy-Long-Legs")); Iterator it = bookShelf.iterator(); while (it.hasNext()) { Book book = (Book)it.next(); System.out.println(book.getName()); } } }
你可以尝试使用Lambda表达式来简化代码,如下所示:
public class Main {
public static void main(String[] args) {
BookShelf bookShelf = new BookShelf(4);
bookShelf.appendBook(new Book("Around the World in 80 Days"));
bookShelf.appendBook(new Book("Bible"));
bookShelf.appendBook(new Book("Cinderella"));
bookShelf.appendBook(new Book("Daddy-Long-Legs"));
bookShelf.forEach(book -> System.out.println(book.getName()));
}
}
优化代码public class Main { public static void main(String[] args) { BookShelf bookShelf = new BookShelf(4); bookShelf.appendBook(new Book("Around the World in 80 Days")); bookShelf.appendBook(new Book("Bible")); bookShelf.appendBook(new Book("Cinderella")); bookShelf.appendBook(new Book("Daddy-Long-Legs")); Iterator it = bookShelf.iterator(); while (it.hasNext()) { Book book = (Book)it.next(); System.out.println(book.getName()); } } }的具体实现
这段代码是一个简单的书架类,其中包含了一个迭代器,用于遍历书架上的书籍。具体实现如下:
public class Book {
private String name;
public Book(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public interface Iterator {
public abstract boolean hasNext();
public abstract Object next();
}
public class BookShelf implements Aggregate {
private Book[] books;
private int last = 0;
public BookShelf(int maxsize) {
this.books = new Book[maxsize];
}
public Book getBookAt(int index) {
return books[index];
}
public void appendBook(Book book) {
this.books[last] = book;
last++;
}
public int getLength() {
return last;
}
public Iterator iterator() {
return new BookShelfIterator(this);
}
}
public class BookShelfIterator implements 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() {
Book book = bookShelf.getBookAt(index);
index++;
return book;
}
}
这段代码中,Book类表示一本书,BookShelf类表示一个书架,BookShelfIterator类表示一个迭代器。BookShelf类实现了Aggregate接口,该接口包含了一个iterator方法,用于返回一个迭代器。BookShelfIterator类实现了Iterator接口,该接口包含了hasNext和next方法,用于判断是否还有下一个元素和返回下一个元素。在main方法中,我们创建了一个BookShelf对象,并向其中添加了四本书,然后使用迭代器遍历了书架上的所有书籍并打印出它们的名称。