简体   繁体   English

如何在类的ArrayList中搜索字符串

[英]How to search for a string inside an ArrayList of a class

/**
 * @param title title of the movie to search for
 * @return the movie with the given title 
 * or null if there is no movie in the library with that title
 */
public Movie findMovieByTitle(String title)
{
    for (Movie movie : movies){
        if(movies.contains(title)){
        return title;
    }
    else{
        return null;
    }
}
}

How can I find the title in the ArrayList? 如何在ArrayList中找到标题? There are similar topics to this but they deal with a String ArrayList, I'm searching for a title in another class. 有与此类似的主题,但它们处理的是String ArrayList,我在另一个类中搜索标题。 Do I need to make a local variable and call a method from the Movie class? 我需要制作一个局部变量并从Movie类中调用一个方法吗?

This is the Movie class: 这是电影类:

public class Movie
{
    private String title;
    private int runLength;  // length of the movie in minutes
    private int starRating; // rating from 1 to 4 stars (0 = not rated yet)

/**
 * Create a movie with the title, run length, and number of stars given
 * @param aTitle title of the movie
 * @param aRunLength run length of the movie in minutes
 * @param aStarRating star rating of the movie, a value from 1 to 4 inclusive
 */
public Movie(String aTitle, int aRunLength, int aStarRating)
{
    title = aTitle;
    runLength = aRunLength;
    setStarRating(aStarRating);
}

/**
 * Create a movie with the title and run length given
 * @param aTitle title of the movie
 * @param aRunLength run length of the movie in minutes
 */
public Movie(String aTitle, int aRunLength)
{
    title = aTitle;
    runLength = aRunLength;
    starRating = 0;
}

/**
 * @return title of the movie
 */
public String getTitle()
{
    return title;
}

/**
 * @return run length of the movie in minutes
 */
public int getRunLength()
{
    return runLength;
}

/** 
 * @return rating in stars of the movie; a value of 0 means "not rated"
 */
public int getStarRating()
{
    return starRating;
}

/**
 * Set the star rating of this movie to the value given
 * @param newRating new star rating of the movie
 */
public void setStarRating(int newRating)
{
    if (newRating >= 1 && newRating <= 4) {
        starRating = newRating;
    }
    else {
        System.out.println("Error: Valid range for star rating is 1 to 4");
    }
}

/**
 * Reset the star rating of this movie to "not rated"
 */
public void resetStarRating()
{
    starRating = 0;
}

/**
 * Increase the star rating of this movie by 1
 */
public void increaseStarRating()
{
    if (starRating < 4) {
        starRating = starRating + 1;
    }
}

/**
 * Decrease the star rating of this movie by 1
 */
public void decreaseStarRating()
{
    if (starRating > 1) {
        starRating = starRating - 1;
    }
}

/**
 * Print information on this movie
 */
public void printMovieInfo()
{
    System.out.println("---------------------------------");
    System.out.println(title);
    System.out.println("Run length: " + runLength);
    if (starRating == 0) {
        System.out.println("Rating: (no rating)");
    }
    else {
        System.out.println("Rating: " + starRating + " stars");
    }
    System.out.println("---------------------------------");
}

} }

public Movie findMovieByTitle(String title) {
    return movies.stream() // open a stream on the movies collection
        .filter(movie -> movie.getTitle().equals(title)) // filter them based on their title
        .findFirst() // get the first matching item
        .get(); // this might throw NoSuchElementException if no element found
}

Try this. 尝试这个。 It should work 它应该工作

public Movie findMovieByTitle(String title)
{
    for (Movie movie : movies){
        if(movie.getTitle().equals(title)){
        return movie;
}
}
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM