我在java中显示数组列表时遇到问题。该程序应该从数组列表中调用一类电影。这是我的代码。
public class Movie {
private String title;
private String category;
public Movie(String title, String category) {
this.title = title;
this.category = category;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
/
public class MovieDB {
private static ArrayList<Movie> allMovies = new ArrayList<>(100);
public static ArrayList<Movie> getAllMovies() {
allMovies.add(new Movie("Citizen Kane", "drama"));
allMovies.add(new Movie("Casablanca", "drama"));
allMovies.add(new Movie("The Godfather", "drama"));
allMovies.add(new Movie("Gone With The Wind", "drama"));
allMovies.add(new Movie("Lawrence Of Arabia", "drama"));
allMovies.add(new Movie("The Wizard Of Oz", "musical"));
allMovies.add(new Movie("The Graduate", "drama"));
allMovies.add(new Movie("On The Waterfront", "drama"));
allMovies.add(new Movie("Schindler's List", "drama"));
allMovies.add(new Movie("Singin' In The Rain", "musical"));
allMovies.add(new Movie("It's A Wonderful Life", "drama"));
allMovies.add(new Movie("Sunset Boulevard", "drama"));
allMovies.add(new Movie("The Bridge On The River Kwai", "drama"));
allMovies.add(new Movie("Some Like It Hot", "drama"));
allMovies.add(new Movie("Star Wars", "scifi"));
allMovies.add(new Movie("All About Eve", "drama"));
allMovies.add(new Movie("The African Queen", "drama"));
allMovies.add(new Movie("Psycho", "horror"));
allMovies.add(new Movie("Chinatown", "drama"));
allMovies.add(new Movie("One Flew Over The Cuckoo's Nest", "drama"));
allMovies.add(new Movie("The Grapes Of Wrath", "drama"));
allMovies.add(new Movie("2001: A Space Odyssey", "scifi"));
allMovies.add(new Movie("The Maltese Falcon", "drama"));
allMovies.add(new Movie("Raging Bull", "drama"));
allMovies.add(new Movie("E.T. The extra-terrestrial", "scifi"));
allMovies.add(new Movie("Dr. Strangelove", "drama"));
allMovies.add(new Movie("Bonnie And Clyde", "drama"));
allMovies.add(new Movie("Apocalypse Now", "drama"));
allMovies.add(new Movie("Mr. Smith Goes to Washington", "drama"));
allMovies.add(new Movie("The Treasure Of The Sierra Madre", "drama"));
allMovies.add(new Movie("Annie Hall", "comedy"));
allMovies.add(new Movie("The Godfather Part II", "drama"));
allMovies.add(new Movie("High Noon", "drama"));
allMovies.add(new Movie("To Kill A Mockingbird", "drama"));
allMovies.add(new Movie("It Happened One Night", "drama"));
allMovies.add(new Movie("Midnight Cowboy", "drama"));
allMovies.add(new Movie("The Best Years Of Our Lives", "drama"));
allMovies.add(new Movie("Double Indemnity", "drama"));
allMovies.add(new Movie("Doctor Zhivago", "drama"));
allMovies.add(new Movie("North By Northwest", "drama"));
allMovies.add(new Movie("West Side Story", "musical"));
allMovies.add(new Movie("Rear Window", "drama"));
allMovies.add(new Movie("King Kong", "horror"));
allMovies.add(new Movie("The Birth Of A Nation", "drama"));
allMovies.add(new Movie("A Streetcar Named Desire", "drama"));
allMovies.add(new Movie("A Clockwork Orange", "scifi"));
allMovies.add(new Movie("Taxi Driver", "drama"));
allMovies.add(new Movie("Jaws", "horror"));
allMovies.add(new Movie("Snow White And The Seven Dwarfs", "animated"));
allMovies.add(new Movie("Butch Cassidy And The Sundance Kid", "drama"));
allMovies.add(new Movie("The Philadelphia Story", "drama"));
allMovies.add(new Movie("From Here To Eternity", "drama"));
allMovies.add(new Movie("Amadeus", "drama"));
allMovies.add(new Movie("All Quiet On The Western Front", "drama"));
allMovies.add(new Movie("The Sound Of Music", "musical"));
allMovies.add(new Movie("M*A*S*H", "comedy"));
allMovies.add(new Movie("The Third Man", "drama"));
allMovies.add(new Movie("Fantasia", "animated"));
allMovies.add(new Movie("Rebel Without A Cause", "drama"));
allMovies.add(new Movie("Raiders Of The Lost Ark", "drama"));
allMovies.add(new Movie("Vertigo", "drama"));
allMovies.add(new Movie("Tootsie", "comedy"));
allMovies.add(new Movie("Stagecoach", "drama"));
allMovies.add(new Movie("Close Encounters Of The Third Kind", "scifi"));
allMovies.add(new Movie("The Silence Of The Lambs", "horror"));
allMovies.add(new Movie("Network", "drama"));
allMovies.add(new Movie("The Manchurian Candidate", "drama"));
allMovies.add(new Movie("An American In Paris", "drama"));
allMovies.add(new Movie("Shane", "drama"));
allMovies.add(new Movie("The French Connection", "drama"));
allMovies.add(new Movie("Forrest Gump", "drama"));
allMovies.add(new Movie("Ben-Hur", "drama"));
allMovies.add(new Movie("Wuthering Heights", "drama"));
allMovies.add(new Movie("The Gold Rush", "drama"));
allMovies.add(new Movie("Dances With Wolves", "drama"));
allMovies.add(new Movie("City Lights", "drama"));
allMovies.add(new Movie("American Graffiti", "drama"));
allMovies.add(new Movie("Rocky", "drama"));
allMovies.add(new Movie("The Deer Hunter", "drama"));
allMovies.add(new Movie("The Wild Bunch", "drama"));
allMovies.add(new Movie("Modern Times", "drama"));
allMovies.add(new Movie("Giant", "drama"));
allMovies.add(new Movie("Platoon", "drama"));
allMovies.add(new Movie("Fargo", "drama"));
allMovies.add(new Movie("Duck Soup", "comedy"));
allMovies.add(new Movie("Mutiny On The Bounty", "drama"));
allMovies.add(new Movie("Frankenstein", "horror"));
allMovies.add(new Movie("Easy Rider", "drama"));
allMovies.add(new Movie("Patton", "drama"));
allMovies.add(new Movie("The Jazz Singer", "drama"));
allMovies.add(new Movie("My Fair Lady", "musical"));
allMovies.add(new Movie("A Place In The Sun", "drama"));
allMovies.add(new Movie("The Apartment", "drama"));
allMovies.add(new Movie("Goodfellas", "drama"));
allMovies.add(new Movie("Pulp Fiction", "drama"));
allMovies.add(new Movie("The Searchers", "drama"));
allMovies.add(new Movie("Bringing Up Baby", "drama"));
allMovies.add(new Movie("Unforgiven", "drama"));
allMovies.add(new Movie("Guess Who's Coming To Dinner", "drama"));
allMovies.add(new Movie("Yankee Doodle Dandy", "musical"));
return allMovies;
}
public static ArrayList<Movie> getMovies(String category) {
ArrayList<Movie> movies = new ArrayList<>();
return (ArrayList<Movie>) getAllMovies().stream().filter(t->t.getCategory().equals(category)).collect(Collectors.toList());
}
public static List<Movie> getMovies1(String category) {
// TODO: add code to add all movies with the specified category
List<Movie> movies= new ArrayList<>();
for (Movie movie : getAllMovies()) {
if (movie.getCategory().equals(category)){
movies.add(movie);
}
}
return movies;
}
@Override
public String toString() {
return "Movie{" +
"title='" + title + '\'' +
", category='" + category + '\'' +
'}';
}
}
/
public class MovieApp {
public static void main(String[] args) {
System.out.println("Welcome to the Movie Application.");
System.out.println("There are 100 movies in the list.");
System.out.println("What category are you interested in?");
System.out.println();
//Create Scanner object
Scanner sc = new Scanner(System.in);
//Initialize variables
String choice="y";
//Create a while loop to continue until user enters no.
while(choice.equalsIgnoreCase("y"))
{
//Get the input from the user
String movieList = Validator.getString(sc,
"Animated, Drama, Horror, SciFi \n"
+ "Please enter a category: ");
//Get the movie object
List<Movie> movie = MovieDB.getMovies(movieList);
//Display the output
System.out.println(); //print blank line
System.out.println("Movie Category: " + movie);
//See if the user wants to continue
choice = Validator.getString(sc, "Continue: (y/n): ");
System.out.println();
}
}
/
public class Validator {
public static String getString(Scanner sc, String prompt){
System.out.print(prompt);
String s = sc.next(); //Reads the user entry
sc.nextLine(); //Discards any other data on the line
return s;
}
public static String getLine(Scanner sc, String prompt){
System.out.print(prompt);
String s = sc.nextLine(); // read the whole line
return s;
}
我在使用MovieApp课程时遇到了问题。
//获取电影对象 ArrayList movie = MovieDB.getMovies(movieList);
这一行具体。无论我投入什么,我都会得到每一部电影和这个任务11.Movie@55f96302
或只是[]。
我不确定如何显示类别列表。
我希望它显示如下
安妮霍尔 中号的 A S * H 窈窕淑男 鸭汤
答案 0 :(得分:0)
根据您的需要,您可能想要
Map<Category, List<Movie>>
然后找到一个类型中的所有动作
将是微不足道的答案 1 :(得分:0)
为了实现getMoviesByCategory功能,我建议你应该使用更好的dataStructure。如:
Map<String, List<Movie>> movies = new HashMap<String, <List>Movie>();
if(movies.get("musical") == null ){
movies.add(new ArrayList().add(new Movie("My Fair Lady", "musical")));
}else{
movies.get("musical").add(new Movie("My Fair Lady", "musical"));
}
然后在
public static ArrayList<Movie> getMovies(String category) {
return movies.get(category);
}
除此之外,覆盖Movie类中的toString()以正确打印它。
答案 2 :(得分:0)
我更改了方法声明并从ArrayList返回到List,这通常更好地使用接口。
在该方法中,您只需要遍历元素并过滤它们:
Java8
public static List<Movie> getMovies(String category) {
// TODO: add code to add all movies with the specified category
return getAllMovies().stream().filter(t->t.getCategory().equals(category)).collect(Collectors.toList());
}
每个
public static List<Movie> getMovies(String category) {
// TODO: add code to add all movies with the specified category
List<Movie> movies= new ArrayList<>();
for (Movie movie : getAllMovies()) {
if (movie.getCategory().equals(category)){
movies.add(movie);
}
}
return movies;
}
最后为了避免assignment11.Movie@55f96302,你需要实现一个toString():
@Override
public String toString() {
return "Movie{" +
"title='" + title + '\'' +
", category='" + category + '\'' +
'}';
}
正如其他人指出的那样,地图是存储数据的更好选择,使用列表中每个元素都需要通过列表来获取电影,而使用地图,您只需要执行此操作一旦构建它,然后查询是不变的。