【问题标题】:How to search for a string inside an ArrayList of a class如何在类的 ArrayList 中搜索字符串
【发布时间】:2016-03-01 00:06:25
【问题描述】:
/**
 * @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;
    }
}
}

如何在 ArrayList 中找到标题?有与此类似的主题,但它们处理字符串 ArrayList,我正在寻找另一个类中的标题。我需要创建一个局部变量并从 Movie 类中调用一个方法吗?

这是电影类:

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("---------------------------------");
}

}

【问题讨论】:

  • if (movie.getTitle().equals(title)?你不应该返回Movie而不是title吗?
  • 返回语句是true吗?
  • 你的方法定义应该像 public string findMovieByTitle(String title) -- 因为它返回的是 String 而不是 Movie。
  • 你用的是什么java版本?
  • 如果 movies 是一个 ArrayList,你可能会丢失 for 循环。

标签: java search arraylist


【解决方案1】:
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
}

【讨论】:

  • 严格来说这不会编译,因为findFirst 返回Optional&lt;T&gt; - 这是为了处理所有其他答案都在努力解决的null 问题。所以需要更改返回类型或者调用get
【解决方案2】:

试试这个。它应该工作

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

【讨论】:

  • 谢谢!我有类似的东西,但我有一个带有 return null; 的 else 声明,但只要把它拿出来就可以了
  • 我建议使用Exception 而不是使用null - 如果可能的话,应该避免在现代Java 中使用null
猜你喜欢
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
相关资源
最近更新 更多