- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一些问题,想不出更多的解决方案。
我希望在用户尝试添加列表中的电影名称和剧院名称时显示结果,它会显示另一个用户输入(年、月、日、小时、分钟)。
但如果列表找不到任何名称/标题匹配,它会打印出“找不到您的电影或/和剧院。”
这是我到目前为止输入的代码。
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] 中的第一个索引,这是可能的,但我似乎无法将它与其他索引进行比较。
假设电影和剧院名称有“a”和“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");
}
}
}
最佳答案
你得到所有这些错误语句的原因,即使在两个列表中都有一个匹配,你正在为每个不匹配的地方打印它们,在 else if
语句中。相反,您应该做的是,获取一个 boolean
变量 found
,将其初始化为 false
。现在,只需在 for
循环中加入 if
block 。如果满足 if
条件,则将 found
变量重置为 true
,并从循环中break
。
在循环外,检查found
的值,如果为false
,则表示没有匹配项。
这是关于您的方法的建议。但是,我会在这里采用不同的方法。
由于您使用的是 ArrayList
,因此您无需手动遍历它来查找是否存在匹配项。您可以为此使用 ArrayList#contains(Object)
方法。该方法内部使用 equals()
方法进行比较,因此我将重写 Movie 中的
和 equals()
和 hashCode()
方法Theatre
类。
下面是 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.");
}
关于Java - 从多个 ArrayList 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350603/
将一个数组列表分配给另一个数组列表与在两个数组列表之间使用 addAll 方法有什么区别? 1 > arrayList = arrayList;//应该将后面的arrayList的值分配给第一个。 2
所以我在将 ArrayList 添加到我的 ArrayList 时遇到了一些问题。将其想象成一张 table 。 下面是一些示例代码: ArrayList currentRow = new Arra
我一直在尝试转换 ArrayList> to ArrayList> 的字符串 这是我尝试构建的代码。 public void convertString (ArrayList> templist) {
我有一个 ArrayList (alal) 和一个 ArrayList(al) 的 ArrayList。我想将 al 插入 alal,但我希望 alal.get(0) 包含 al 拥有的所有内容以及添
很抱歉标题太长,如果您能想到更好的标题,请告诉我! 我正在做的是尝试创建一个 ArrayList 的 ArrayList 并将 ArrayList 逐个添加到其中。两个AL>我所拥有的称为三角形和正方
我有带有值的 mList2。存在具有相同 id 的值。如何获取具有相同 id 的对象分组的 List 或 ArrayList 并将其添加到 ArrayList>? List mList2 = list
我正在创建一个 ArrayList的 ArrayList并添加 ArrayLists给它。但每次我对 ArrayList 进行更改时, 它反射(reflect)在 ArrayList 中. 示例: L
谁能解释一下ArrayList之间的区别是什么? , ArrayList和 ArrayList是什么时候使用它们?它们在实现层面上是相同的还是各自具有不同的含义? 最佳答案 ArrayList 特别是
这个问题在这里已经有了答案: Java generics: List> = new LinkedList>() is prohibited? (3 个答案) 关闭 9 年前。 为什么这段代码可以编译
我的 arraylistS 在覆盖数组列表中的行为类似于同一个实例。 我用其中一个来操作 i=0; manupulate((ArrayList)theCoveringRootArrayList.get
我们遇到这个错误 java.lang.NullPointerException at java.util.ArrayList.(Unknown Source) at de.mystuf
据我了解,ArrayList 类继承其父“List”类的 equals() 函数来查找两个成员对象是否相同。这是否意味着“contains()”线性搜索(使用“equal”)来查找 ArrayList
这个问题已经有答案了: What is the diamond operator in Java? (2 个回答) 已关闭 7 年前。 正如标题所说,在Java中,这两种语句有什么区别吗? 通常我都能
我正在尝试求解帕斯卡三角形。我有两个用 Java 编写的代码片段,第一个创建 inner ArrayList 几次并且对我来说效果很好。 但是在代码的第二个版本中,如果我修改 inner ArrayL
正如标题所示,我有两个 ArrayList。奇怪的是,在一个数组列表上设置一个值会改变另一个数组列表的值。 一些信息:这些是 Entry 类型的 ArrayList,每个列表都包含一个金额和一个值(这
我已经添加了一个项目到列表 a,然后添加了列表 a 到列表 b 并再次做了同样的事情。 我的问题是,如果我打印 b.get(0) 和 b.get(1),我会得到相同的列表,这两个项目都是 “一”和“二
我正在创建一个 ArrayList of ArrayList of ArrayList 的 ArrayList 并按以下方式填充它。它正确地填充它。我已经通过调试和 println 弄清楚了这一点。但
实现可以在 Arraylist 和 Integer 中存储任何级别的 ArrayList 的 ArrayList 的最佳方法是什么。 List> list = ArrayList(); 仅允许列表中最
在下面的示例中,我将如何将 ArrayList al4 的内容与其他 ArrayList 中的任何一个进行比较?以同样的方式,我将 al1 与 al2 进行了比较。 import java.util.
好的,所以我之前发布了一个线程,它回答了我的很多问题并帮助我改进了我的代码,但是,我遇到了另一个问题,我不知道为什么,但我认为也许该副本只是指向原始对象..(尽管我已尽力避免这种情况) 在我的游戏代码
我是一名优秀的程序员,十分优秀!