设计一个类MovieGenerator,可用于将电影片名填充不同类型的容器 类中包含一成员,电影片名的声明如下: static String movi串。 设计方法实现对电影片名的处理:1. 一个生成方法next,可以获取下一部电影片名,其中如果已获取完毕所有的电影名,则循环到第一部影片名开始 ,如next(String[] array),next(Collection<String> collection) 2. 一个填充方法fill;3.分别打印每一个容器或数组中的所有内容
时间: 2023-06-13 17:08:46 浏览: 169
c语言题目-c语言题目
以下是我为您设计的MovieGenerator类:
```java
import java.util.Collection;
public class MovieGenerator {
private static final String[] MOVIES = {
"The Shawshank Redemption",
"The Godfather",
"The Dark Knight",
"Schindler's List",
"Forrest Gump",
"Star Wars: Episode IV - A New Hope",
"The Lord of the Rings: The Fellowship of the Ring",
"The Matrix",
"Jurassic Park",
"Titanic"
};
private int currentIndex;
public String next() {
String nextMovie = MOVIES[currentIndex];
currentIndex = (currentIndex + 1) % MOVIES.length;
return nextMovie;
}
public void fill(String[] array) {
for (int i = 0; i < array.length; i++) {
array[i] = next();
}
}
public void fill(Collection<String> collection) {
collection.clear();
for (int i = 0; i < MOVIES.length; i++) {
collection.add(next());
}
}
public void print(String[] array) {
for (String movie : array) {
System.out.println(movie);
}
}
public void print(Collection<String> collection) {
for (String movie : collection) {
System.out.println(movie);
}
}
}
```
其中,MOVIES数组包含了一些电影片名,next()方法用于获取下一部电影片名,并会循环到第一部电影片名开始。fill(String[] array)方法用于将电影片名填充到一个数组中,fill(Collection<String> collection)方法用于将电影片名填充到一个集合中。print(String[] array)方法用于打印数组中的所有内容,print(Collection<String> collection)方法用于打印集合中的所有内容。
使用示例:
```java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
MovieGenerator generator = new MovieGenerator();
// 使用数组
String[] array = new String[10];
generator.fill(array);
generator.print(array);
// 使用集合
List<String> list = new ArrayList<>();
generator.fill(list);
generator.print(list);
}
}
```
输出:
```
The Shawshank Redemption
The Godfather
The Dark Knight
Schindler's List
Forrest Gump
Star Wars: Episode IV - A New Hope
The Lord of the Rings: The Fellowship of the Ring
The Matrix
Jurassic Park
Titanic
The Shawshank Redemption
The Godfather
The Dark Knight
Schindler's List
Forrest Gump
Star Wars: Episode IV - A New Hope
The Lord of the Rings: The Fellowship of the Ring
The Matrix
Jurassic Park
Titanic
```
阅读全文