JAVA:在arraylist中搜索项只返回第一个项
我有一个Java程序,要求用户输入五个字符串。我的目标是在包含“a”的列表中搜索特定元素。在运行代码时可以看到,它只返回包含元素“a”的字符串的第一个匹配项。例如:(阿莱雅,阿莱克斯,亚瑟,约翰,怪诞) 获得的输出:aleah 预期输出:它应该返回aleah、alex和arthur 我已经把我的代码贴在下面,请看一下JAVA:在arraylist中搜索项只返回第一个项,java,arraylist,data-structures,Java,Arraylist,Data Structures,我有一个Java程序,要求用户输入五个字符串。我的目标是在包含“a”的列表中搜索特定元素。在运行代码时可以看到,它只返回包含元素“a”的字符串的第一个匹配项。例如:(阿莱雅,阿莱克斯,亚瑟,约翰,怪诞) 获得的输出:aleah 预期输出:它应该返回aleah、alex和arthur 我已经把我的代码贴在下面,请看一下 public ArrayList<String> searchName(String sn){ // String sn is the the search for
public ArrayList<String> searchName(String sn){
// String sn is the the search for character value (e.g 'a' or 'aa')
ArrayList<String> searches = new ArrayList<>();
for(String n : names){
// names list is declared in the other method and handles all the name values
// loop through the list. Am i using the right loop?
if(n.contains(sn)){
// if a specific index in the list contains the sn
searches.add(n);
// stored in in the new list searches
displaySearches(searches);
// called displaySearches and passed the arralist searches.
}
else
System.out.println("No results for " + sn);
break;
}
return searches;
}
public void displaySearches(ArrayList<String> searches){
for(String s: searches){
System.out.println(s);
// populates all the search results from the list.
}
}
public ArrayList searchName(字符串sn){
//字符串序列号是搜索字符值(例如“a”或“aa”)时使用的序列号
ArrayList searches=新建ArrayList();
对于(字符串n:名称){
//名称列表在另一个方法中声明,并处理所有名称值
//在列表中循环。我是否使用了正确的循环?
如果(n.包含(sn)){
//如果列表中的特定索引包含序列号
添加(n);
//存储在新列表搜索中
显示搜索(搜索);
//调用DisplaySearchs并传递arralist搜索。
}
其他的
System.out.println(“无“+sn”结果);
打破
}
返回搜索;
}
公共void显示搜索(ArrayList搜索){
用于(字符串s:搜索){
系统输出打印项次;
//填充列表中的所有搜索结果。
}
}
问题在于:
else
System.out.println("No results for " + sn);
break;
设置break
语句意味着它将中断整个循环,不检查任何进一步的项-它总是在数组的第一个字符串之后执行此操作,因为您没有为else
放任何括号
删除
中断
应该可以解决您的问题。您的else
语句在这里产生了问题。只需将其移除或将其置于循环外部即可
你可以这样做
public ArrayList<String> searchName(String sn){
ArrayList<String> searches = new ArrayList<>();
//1. First add all the matches to the `searches` List.
for(String n : names){
if(n.contains(sn)){
searches.add(n);
}
}
//2. Now if the `searches` list is NOT empty, it means that
// there were some matches, then display those matches
if(!searches.isEmpty()) {
displaySearches(searches);
} else {
//3. Otherwise print that there wasn't any match.
System.out.println("No results for " + sn);
}
return searches;
}
public void displaySearches(ArrayList<String> searches){
for(String s: searches){
System.out.println(s);
}
}
public ArrayList searchName(字符串sn){
ArrayList searches=新建ArrayList();
//1.首先将所有匹配项添加到“搜索”列表中。
对于(字符串n:名称){
如果(n.包含(sn)){
添加(n);
}
}
//2.如果“搜索”列表不是空的,则表示
//存在一些匹配项,然后显示这些匹配项
如果(!searches.isEmpty()){
显示搜索(搜索);
}否则{
//3.否则,请打印不存在任何匹配项。
System.out.println(“无“+sn”结果);
}
返回搜索;
}
公共void显示搜索(ArrayList搜索){
用于(字符串s:搜索){
系统输出打印项次;
}
}
如果要查找包含“A”的所有字符串,则应删除“else”部分。因为在else之后不使用括号,所以由于break语句,第二个n值不会被检查
我建议您删除代码的其他部分并再次检查。此外,循环完成后可以调用display函数。因为您在else条件之后中断了循环
public ArrayList<String> searchName(String sn){
// String sn is the the search for character value (e.g 'a' or 'aa')
ArrayList<String> searches = new ArrayList<>();
for(String n : names){
// names list is declared in the other method and handles all the name values
// loop through the list. Am i using the right loop?
if(n.contains(sn)){
// if a specific index in the list contains the sn
searches.add(n);
// stored in in the new list searches
displaySearches(searches);
// called displaySearches and passed the arralist searches.
}
}
if(searches.isEmpty()){
System.out.println("No results for " + sn);
}else{
displaySearches(searches);
}
return searches;
}
public void displaySearches(ArrayList<String> searches){
for(String s: searches){
System.out.println(s);
// populates all the search results from the list.
}
}
public ArrayList searchName(字符串sn){
//字符串序列号是搜索字符值(例如“a”或“aa”)时使用的序列号
ArrayList searches=新建ArrayList();
对于(字符串n:名称){
//名称列表在另一个方法中声明,并处理所有名称值
//在列表中循环。我是否使用了正确的循环?
如果(n.包含(sn)){
//如果列表中的特定索引包含序列号
添加(n);
//存储在新列表搜索中
显示搜索(搜索);
//调用DisplaySearchs并传递arralist搜索。
}
}
if(searches.isEmpty()){
System.out.println(“无“+sn”结果);
}否则{
显示搜索(搜索);
}
返回搜索;
}
公共void显示搜索(ArrayList搜索){
用于(字符串s:搜索){
系统输出打印项次;
//填充列表中的所有搜索结果。
}
}
您的else
块不应在for循环中。break
指令停止循环两个问题,在每次搜索匹配后调用display,第二个break
如果不包含(删除break部分并在搜索完成后显示)是的,谢谢,我现在得到所有带有“a”的项目。但是,我返回了重复项。输入(ayla aranda aleah roco-Jerie)输出(ayla aranda ayla aranda aleah)@JongOnin使用集合而不是列表来消除重复项。我已经删除了else块和break语句。我现在已从带有“a”的列表中检索到项目。但是,显示的项目重复。样本运行(alessa alessa alexa alessa andrea)是。由于从for
循环中调用displaySearches()
方法,所以您得到了重复项。这就是为什么在我的例子中,我将它保持在循环之外。