首页 > 解决方案 > 比较两个数组以仅检索 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[]。第一个代码片段可以通过一些操作来完成这项工作。

标签: javajsp

解决方案


您需要使用equals方法来比较值而不是contains.Also ,在下面的代码中,我添加了额外的变量来通过将值与数组进行比较found来检查值是否存在,并且取决于它是否会被选中。即: brandslistuserBrandsoptionsselected

 <%
    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>
        <%}
    }%>

推荐阅读