{ "question_id": 7431662, "question_title": "定义一个类Generator", "question_content": "
定义一个类 Generator(生成器类),它可以在每次调用其 next()方法时,产生由你 最喜欢的电影(如 Snow White 或 Star Wars)的字符构成的名字(作为 String 对象)。在字 符列表中的电影名用完之后,循环到这个字符列表的开始处。使用这个生成器来填充数组、 ArrayList、LinkedList、HashSet,然后打印每一个容器。
", "difficulty": "简单", "answer_id": 53400496, "answer_content": "\nimport java.util.*;\npublic class MovieGenerator {\n private String[] movies = new String[] { "SS", "DD", "HH", "FF", "XX", "ZZ" };\n private int i = 0;\n public String next() {\n return movies[i++ % movies.length];\n }\n public String[] getMovies() {\n return movies;\n }\n public Collection<String> fill(Collection<String> collection) {\n for (int i = 0; i < 8; i++) {\n collection.add(next());\n }\n return collection;\n }\n public static void main(String[] args) {\n MovieGenerator generator = new MovieGenerator();\n System.out.println(Arrays.toString(generator.getMovies()));\n System.out.println(generator.fill(new ArrayList<String>()));\n System.out.println(generator.fill(new LinkedList<String>()));\n System.out.println(generator.fill(new HashSet<String>()));\n }\n}
\n\n\n", "tag_name": "java", "java": "import java.util.*;\npublic class MovieGenerator {\n\tprivate String[] movies = new String[] { \"SS\", \"DD\", \"HH\", \"FF\", \"XX\", \"ZZ\" };\n\tprivate int i = 0;\n\tpublic String next() {\n\t\treturn movies[i++ % movies.length];\n\t}\n\tpublic String[] getMovies() {\n\t\treturn movies;\n\t}\n\tpublic Collection