【问题标题】:How to call array list in console java如何在控制台java中调用数组列表
【发布时间】:2017-03-20 17:03:51
【问题描述】:

我在 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 电影 = MovieDB.getMovies(movieList);

这一行。无论我投入什么,我都会得到每部电影和这个作业11.Movie@55f96302

或只是[]。

我不确定如何显示类别列表。

我希望它像这样显示

安妮·霍尔 MAS*H 嘟嘟 鸭汤

【问题讨论】:

标签: java arrays arraylist


【解决方案1】:

根据您可能想要做的需求

Map<Category, List<Movie>>

那么找到一个流派中的所有动作将是微不足道的

【讨论】:

    【解决方案2】:

    要实现 getMoviesByCategory 功能,我建议您应该使用更好的 dataStructure.Something 之类的:

    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() 以正确打印。

    【讨论】:

      【解决方案3】:

      我更改了方法声明并从 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 + '\'' +
                  '}';
      }
      

      正如其他人指出的那样,地图是存储数据的更好选择,使用列表每次都需要遍历列表的每个元素才能获取电影,而使用地图,您只需要这样做一次构建它,然后查询是不变的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 2016-03-25
        相关资源
        最近更新 更多