Java - 从多个ArrayList获取值

时间:2014-01-25 12:36:10

标签: java arraylist

我遇到了一些问题,不能再考​​虑解决方案了。

我希望在用户尝试添加电影标题时显示结果。在列表中的剧院名称,它将显示另一个用户输入(年,月,日,小时,分钟)。

但如果列表找不到任何名称/标题匹配,则会打印出“您的电影或/和影院无法找到。”

这是我到目前为止输入的代码。

public void addScreening() {
    System.out.println("-ADD NEW SCREENING-");

    String mTitle = Helper.readString("Enter movie title > ");
    String tName = Helper.readString("Enter theatre name > ");

    for (int i = 0; i < movies.size(); i++) {for (int j = 0; j < theatres.size(); j++) {
            if ((movies.get(i).getTitle().contains(mTitle) && movies.get(i)
                    .getTitle() != null)
                    && (theatres.get(j).getName().contains(tName) && theatres
                            .get(j).getName() != null)) {

                int year = Helper.readInt("Enter year > ");
                int month = Helper.readInt("Enter month > ");
                int day = Helper.readInt("Enter day > ");
                int hour = Helper.readInt("Enter hour > ");
                int min = Helper.readInt("Enter min > ");

                screenings.add(new MovieScreening(Helper.thisDate(year,
                        month, day, hour, min), movies.get(i), theatres
                        .get(j), 0));
                System.out.println("Added successfully");
                break;
            } else if ((!movies.get(i).getTitle().contains(mTitle))
                    || (!theatres.get(j).getName().contains(tName))) {
                System.out
                        .println("Your movie or/and theatre cannot be found.");
                break;
            }
        }
    }
}

如果它比较列表[0]中的第一个索引,它可能,但我似乎无法将它与其他索引进行比较。

让我们看看电影&amp;剧院名称有“a”&amp; “B”。

电影标题:“a”,“b”

剧院名称:“a”,“b”

输出

-Add New Movie Screening-
Enter movie title > a
Enter theatre name > a
// User input
Added Successfully
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
-----------
-Add New Movie Screening-
Enter movie title > a
Enter theatre name > b
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
-----------
-Add New Movie Screening-
Enter movie title > b
Enter theatre name > a
Your movie or/and theatre cannot be found.
// user input
Added successfully
Your movie or/and theatre cannot be found.
-----------
-ADD NEW SCREENING-
Enter movie title > b
Enter theatre name > b
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.
Your movie or/and theatre cannot be found.

通常这个输入应该是成功的,因为列表中有“a”和“b”。

Enter movie title > a
Enter theatre name > b
-----------
Enter movie title > b
Enter theatre name > b

我认为问题在于它无法通过剧院的所有列表。

需要帮助提供有关如何浏览多个列表的提示。

尝试过使用iterator(),但问题也很接近。

public void addScreening() {
    System.out.println("-ADD NEW SCREENING-");

    String mTitle = Helper.readString("Enter movie title > ");
    String tName = Helper.readString("Enter theatre name > ");

    Iterator<Movie> it1 = movies.iterator();
    Iterator<Theatre> it2 = theatres.iterator();

    while(it1.hasNext() && it2.hasNext()){
        Movie a = it1.next();
        Theatre b = it2.next();
        if((a.getTitle().contains(mTitle) ) && (b.getName().contains(tName) )){
            int year = Helper.readInt("Enter year > ");
            int month = Helper.readInt("Enter month > ");
            int day = Helper.readInt("Enter day > ");
            int hour = Helper.readInt("Enter hour > ");
            int min = Helper.readInt("Enter min > ");

            screenings.add(new MovieScreening(Helper.thisDate(year, month, day, hour, min),a,b,0));
            System.out.println("Added successfully");
            break;
        }else{
            System.out.println("Your movie or/and theatre cannot be found. 2");
        }
    }

    }

1 个答案:

答案 0 :(得分:1)

您获取所有这些错误语句的原因是,即使列表中存在匹配项,您也会在else if语句中为每个不匹配项打印它们。而你应该做的是,采用boolean变量found,将其初始化为false。现在,只需在if循环中放置for块。如果满足if条件,请将found变量重置为true,并将break重置为循环。

在循环之外,检查found的值,如果是false,则表示没有匹配。


这是关于你的方法的建议。但是,我会在这里采用不同的方法。

由于您使用的是ArrayList,因此您无需手动迭代它以查找是否存在匹配项。您可以使用ArrayList#contains(Object)方法。该方法内部使用equals()方法进行比较,因此我会覆盖equals()hashCode()类中的MovieTheatre方法。

以下是equals方法的样子:

// Movie:
@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Movie)) return false;

    Movie that = (Movie) obj;

    return this.title.contains(that.title) || that.title.contains(this.title);
}

同样适用于Theatre类:

// Theatre:
@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Theatre)) return false;

    Theatre that = (Theatre) obj;

    return this.name.contains(that.name) || that.name.contains(this.name);
}

然后,将您的for循环替换为:

String mTitle = Helper.readString("Enter movie title > ");
String tName = Helper.readString("Enter theatre name > ");

// Create `Movie` and `Theatre` instance
// Ideally you would not create a new instance. Rather just fetch them from 
// database, or some `static` list, in case of in-memory implementation.
// From your implementation, it seems like the entered value might not be the exact
// match. So that you have to handle.
Movie movie = new Movie(mTitle);
Theatre theatre = new Theatre(tName);

// If you are fetching the movie and theatre from some database or list, then 
// you should just check if `movie` and `theatre` is not `null`
if (movies.contains(movie) && theatres.contains(theatre) {
    int year = Helper.readInt("Enter year > ");
            int month = Helper.readInt("Enter month > ");
            int day = Helper.readInt("Enter day > ");
            int hour = Helper.readInt("Enter hour > ");
            int min = Helper.readInt("Enter min > ");

            screenings.add(new MovieScreening(Helper.thisDate(year,
                    month, day, hour, min), movies.get(i), theatres
                    .get(j), 0));
            System.out.println("Added successfully");
} else {
    System.out.println("Your movie or/and theatre cannot be found.");
}