2013-12-23 78 views
1

這裏是我的值編碼:如何在java中刪除列表中的元素數組?

package Controls; 

// Other code 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
    response.getWriter().write("<h5 class='searchResultFramePara'>Assigned roles</h5>"); 
    String[] Removelist = request.getParameterValues("deleterolelist"); 
//String[] Removelist=removelist.split(","); 
    //System.out.println(Removelist); 
    String username = request.getParameter("name"); 
    //System.out.println(username); 
    List<String> Removelist1 = new ArrayList<String>(); 
    Connection Con = null; 
    Statement st = null; 
    ResultSet rs; 
    int rs1; 
    try { 
     Con = LoginSrv1.getConnection(); 
     st = Con.createStatement(); 
     String roles_query = "SELECT ROLE_NAME FROM demo.ZMM_USER_GROUP WHERE USER_NAME = '" + username + "';"; 
     rs = st.executeQuery(roles_query); 
     while (rs.next()) { 
      String assigned_role = rs.getString("ROLE_NAME"); 
      String[] assigned_array = assigned_role.split(","); 
      if (assigned_array != null) { 
       for (int i = 0; i < assigned_array.length; i++) { 
        Removelist1.add(assigned_array[i]); 
       } 
      } 
     } 
     for (int j = 0; j < Removelist.length; j++) { 
      if (Removelist1.contains(Removelist[j])) { 
       Removelist1.remove(Removelist[j]); 
      } 
     } 
     System.out.println(Removelist1); 

     StringBuilder b = new StringBuilder(); 
     for (String text1 : Removelist1) { 
      b.append(text1).append(","); 
     } 

     String Roletext = b.toString().replaceAll(",$", ""); 
     System.out.println(Roletext); 
     String update_query = "UPDATE DEMO.ZMM_USER_GROUP SET ROLE_NAME='" + Roletext + "' WHERE USER_NAME = '" + username + "';"; 
     rs1 = st.executeUpdate(update_query); 
     String assignedrole_query = "SELECT ROLE_NAME FROM demo.ZMM_USER_GROUP WHERE USER_NAME = '" + username + "';"; 
     rs = st.executeQuery(assignedrole_query); 
     while (rs.next()) { 

      String rolename = rs.getString("ROLE_NAME"); 
      String[] sarr = rolename.split(","); 
      for (int i = 0; i < sarr.length; i++) { 
       //System.out.println("array"+i+sarr[i]); 
       response.getWriter().write("<a href='javascript:void(0);' id=" + sarr[i] + " class='availalbe' onclick='removelist(\"" + sarr[i] + "\");'>" + sarr[i] + "</a><br>"); 
      } 
     } 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
} 

在這裏,我得到Removelist陣列和角色名來自阿賈克斯的請求。現在我刪除Removelist1列表中的removelist。例如我發送具有一個字符串值「發起者」的Removelist,然後它將被刪除。但是,例如它包含「發起者」,「生產管理者」(即,數組包含多個值),那麼它不會刪除它。

+3

有很多的代碼在這裏,如果你發佈你會得到更多的幫助短的,獨立的,正確的(可編譯),實例 - http://sscce.org/ – SimonC

+1

如果我正確地得到你的意思,你的意思是你有一些需要在其他一些(Removelist)集合中刪除的數據的集合。如果是這種情況,你可以使用ArrayList的removeAll()。 – guptakvgaurav

+0

「不會刪除」是什麼意思? remove()拋出異常,或返回false,什麼?對於(int j = 0; j

回答

1

使用

for(int j=0;j<Removelist.length;j++) 
{   
boolean isRemoved = Removelist1.remove(Removelist[j]); 
if(isRemoved){ 
    //Removelist[j] is removed 
}else{ 
    //List does not contain element Removelist[j] 
}    
} 
+0

我想刪除一個元素數組,但它會刪除一個元素數組,但它只會刪除一個值。 – user3016102

+2

由於調用remove()會覆蓋數組的所有值,因此如果list將包含數組中存在的任何值,則該值將被刪除。我想你首先要弄清楚列表中存在的值是什麼。第二,數組中的值是什麼。 – guptakvgaurav

+0

Removelist1 = [ADMIN,INITIATOR,FULFILLMENT,SUPPLY PLANNING,PRINT PRODUCTION,PROJECT MANAGEMENT,SALES,MARKETING] – user3016102

相關問題