java中list.remove方法使用

来源:百度知道 编辑:UC知道 时间:2024/05/07 08:32:14
public List updateProduct(List lists,String[] productId1)throws Exception{
Connection conn=null;
PreparedStatement prep=null;
ResultSet rs=null;
List all=new ArrayList();
for (int i=0;i<productId1.length;i++)
{

lists.remove(new Integer(productId1[i]));

}
System.out.println();
try{
for(int i=0;i<lists.size();i++)
{
Product product1=(Product)lists.get(i);
int productId=product1.getProductId();

System.out.println("剩下的商品Id="+productId);

String sql="select * from product where productId ="+productId;
conn= new DBConnection().getConnection();
//System.out.println("sql11111111111111="+sql);
prep=conn.prepareStatement(sql);
rs = prep.executeQuery();
while (rs.next()){
Product product=new Product();

因为list是有顺序的,先add的编号就小(从0开始),这样就可以通过remove(编号)的形式进行删除,之后后面的会编号依次变小(也就是说编号总是连续的)。举例:
List list = new linkedList();
list.add("0");
list.add("1");
list.remove(0);
结果就是:list.get(0) =1;
备注:如果再一次“list.remove(0);”那么list对象就是个空。

/*我又改了改,哎,还要在学习啊,^_^*/
public List updateProduct(List lists,String[] productId1) throws Exception{
Connection conn=null;
PreparedStatement prep=null;
ResultSet rs=null;
List all=new ArrayList();
/*
for (int i=0;i<productId1.length;i++)
{
lists.remove(new Integer(productId1[i]));
}
*/
for(int i=0;i<productId1.length;i++) {
String pid=productId1[i].trim();
for(int j=0;j<lists.size();j++) {
String oneid=(String)lists.get(j);
oneid=oneid.trim();
if(pid.equals(oneid)){
lists.remove(j);
System.out.println("sxp debug:remove the id "+oneid);
break;
}
}
}

Syst