java - 比较两个数组以仅检索 JSP 中的完全匹配,同时保留下拉菜单中的显示顺序
问题描述
我想将userBrands显示为选项窗格中的选定元素,并在下拉列表中显示完整的品牌列表。当brands.contains(brand.toLowerCase().trim())
检查条件 --> 时,将显示元素 bat、cat 和 batting 而不仅仅是 bat 和 cat。
String brands="";
String userBrands[]=new String[30];
String brandslist[]= {"bat", "cat", "ant", "batting", "antenna"};
if(user.get("brand")!=null && !user.get("brand").isEmpty()){
brands = user.get("brand").toLowerCase();
System.out.println("BRANDS: " + brands); //here bat and cat retrieved from the database //Output: BRANDS: bat, cat
userBrands = brands.split(",");
}
for(String brand:brandslist){
if(brands.contains(brand.toLowerCase().trim())){%>
<option selected="selected" value="<%=brand.trim().toLowerCase()%>"><%=brand.trim()%></option>
<%}else{%>
<option value="<%=brand.trim().toLowerCase()%>"><%=brand.trim()%></option>
<%}
}
如果我尝试使用如下所示的 2 个循环,则不会保持顺序,即选定的循环首先出现在下拉列表中,然后是未选择的循环:
if(user.get("brand")!=null && !user.get("brand").isEmpty()){
brands = user.get("brand").toLowerCase();
System.out.println("BRANDS: " + brands);
userBrands = brands.split(",");
}
for(int p=0; p<brandlist.length; p++){
brandlist[p]=brandlist[p].trim().toLowerCase();
}
for(int q=0; q<userBrands.length; q++){
userBrands[q]=userBrands[q].trim().toLowerCase();
}
List<String> masterList = new ArrayList<String>();
List<String> selectedList = new ArrayList<String>();
List<String> unSelectedList = new ArrayList<String>();
masterList = Arrays.asList(brandlist);
for(int i=0; i<userBrands.length; i++){
for(int j=0; j<brandlist.length; j++){
if(userBrands[i].trim().equalsIgnoreCase(brandlist[j].trim())){
%>
<option selected="selected" value="<%=userBrands[i].trim()%>"><%=userBrands[i].trim()%></option>
<%
}
}
}
selectedList = Arrays.asList(userBrands);
for(String ms: masterList){
unSelectedList.add(ms);
for(String s: selectedList){
if(s.equalsIgnoreCase(ms)){
unSelectedList.remove(s);
}
}
}
for(String unStr: unSelectedList){%>
<option value="<%=unStr.trim()%>"><%=unStr.trim()%></option>
<%}%>
我想要的是只有来自数据库的那些user.get("brand").toLowerCase()
可以作为选定的选项显示在选项中(不是任何额外的——比如第一个代码片段中的击球),并且下拉列表中品牌的显示顺序保持不变原始数组--> brandslist[]
。第一个代码片段可以通过一些操作来完成这项工作。
解决方案
您需要使用equals
方法来比较值而不是contains
.Also ,在下面的代码中,我添加了额外的变量来通过将值与数组进行比较found
来检查值是否存在,并且取决于它是否会被选中。即: brandslist
userBrands
options
selected
<%
String brands="";
String userBrands[];
String brandslist[]= {"bat", "cat", "ant", "batting", "antenna"};
if(user.get("brand")!=null && !user.get("brand").isEmpty()){
brands = user.get("brand").toLowerCase();
System.out.println("BRANDS: " + brands);
userBrands = brands.split(",");
}
//out.println(Arrays.toString(userBrands));
// to check if value there or not
boolean found;
for(int i= 0 ;i<brandslist.length ;i++){
found = false;
for(int j= 0 ;j<userBrands.length ;j++){
if(userBrands[j].equals(brandslist[i].toLowerCase().trim())){ %>
<option selected='selected' value='<%=brandslist[i].toLowerCase().trim()%>'><%=brandslist[i].trim()%></option>
<%
//value there
found = true;
}
}
//value not there found will remain false and below code will get executed
if(!found){ %>
<option value='<%=brandslist[i].trim().toLowerCase()%>'><%=brandslist[i].trim()%></option>
<%}
}%>
推荐阅读
- java - 执行自动化脚本时出现“org.openqa.selenium.WebDriverException:未知错误:由于页面崩溃而删除会话”错误
- node.js - Websockets 严重的延迟/延迟
- node.js - 如何在nodejs中动态创建文件位置
- java - 使用 Spring Security 发布异步方法使会话无效
- r - 在 R 中,为什么白色 geom_point 显示在情节中而不显示在图例中?
- javascript - 打字稿不使用定义呈现 index.d.ts
- python - 运行任何 kivy 代码时出错:Kivy 2.0.0,python 3.9 错误:[CRITICAL] [Window] 无法找到任何有价值的 Window 提供程序
- powerbi - 在 Power PI 仪表板上显示两个表的并集 - DAX 或 M
- javascript - 为什么当我按下可触摸的不透明度时,它会给出一个对象错误作为反应子无效
- excel - 电源查询,vba改源