list循环remove,list循环remove报错

http://www.itjxue.com  2023-01-09 16:04  来源:未知  点击次数: 

如何删除一个list中最后一个元素

1、python删除列表中指定元素的方法。

2、代码实例,创建一个列表。

3、打印定义的列表,使用print()。

4、删除列表中的指定元素,使用remove()。

5、再次打印删除元素后的列表。

6、列表的remove()方法其他注意事项。

遍历list进行remove操作异常

产生问题:

在对list增强for循环进行遍历的时候,如果在不恰当的位置使用了remove,就会产生ConcurrentModificationException异常

产生原因:

java的foreach循环其实就是根据list对象创建一个Iterator迭代对象,然后针对Iterator进行遍历.遍历过程中会调用对应的hasNext和next方法

(1)ArrayList的hasnext和next 方法

? ?(2)LinkedList的hasnext和next 方法

两种list的iterator 都有一个方法是checkForComodification,目的是校验是否list被修改过。list中有个属性是modCount,记录着操作修改list的属性,如果操作list的add或remove,modCount属性都会进行+1的操作。

如果发生了修改,那么在下一次执行到这一步校验的时候,就会发现两个值不相等,抛出异常。

集合list遍历时删除元素,remove中是new integer时不报错

就这一段代码,无法更好的解释。

首先你得把你报错的信息拿出来看看啊。

list.romove(int)这个方法是删除指定位置的元素。所以你这有可能是越界的错误。比如nums数组里有个数字25,但是你的list.size()=10,也就是你的list大小为10,但是你这里删除remove(25),根本就没有第25位。所以报错。

那为什么后面的写法不错呢?那是因为list.remove()方法有另一个重载的方法list.remove(Object)。

你写list.remove(new Integer(num));意思是寻找集合里第一次出现的该元素并删除,比如nums里有个数字25,在这里就是删除list里的25,若果存在的话。不存在返回false不会报错。

总结:两种写法的根本区别就是,第一种是删除第n个位置的某个元素元素,如果list长度为10,你要删除第25位那肯定报错。第二种是删除集合里25这个元素,不是索引

普通for循环遍历List时调用remove方法,List没有遍历完。为什么?

list集合有个特点,比如说一个list里面有三个元素a,b,c,对应的索引分别是0,1,2。当调用remove(0)时,list会移除a这个元素,这个时候list里面的元素就变成b,c,对应的索引值分别为0,1。这个时候list的size已经变成了2,但是如楼主的for循环的话,循环的上限还是最开始的list的size值,也就是3。如此下去每移除一个元素的时候,list的索引取值范围都会变小,最后如果remove(i)中i的值超出了当前集合的范围了,就会报异常

JAVA中LISt遍历时如何remove元素

public

class

RemoveElementDemo

{

public

static

void

main(String[]

args)

{

ListString

list

=

new

ArrayList();

list.add("100012011");

list.add("10001201s1");

list.add("10001201s1");

//解决方案:

//1.i--操作

/*for(int

i

=

0;i

list.size();i++){

String

b

=

list.get(i);

if(b.equals("502323232")){

list.remove(i);

i--;

}

}*/

//2.反向遍历

/*for(int

i

=

list.size()

-

1;i

=

0;i--){

String

b

=

list.get(i);

if(b.equals("502323232")){

list.remove(i);

}

}*/

//解决方案:调用Iterator的remove()方法安全删除元素,避免异常

IteratorString

iter

=

list.iterator();

while(iter.hasNext()){

String

b

=

iter.next();

if(b.equals("100012011")){

iter.remove();

}

}

for(String

b

:

list){

System.out.println(b);

}

}

}

java中list.remove方法使用

/*我又改了改,哎,还要在学习啊,^_^*/

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;iproductId1.length;i++)

{

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

}

*/

for(int i=0;iproductId1.length;i++) {

String pid=productId1[i].trim();

for(int j=0;jlists.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;

}

}

}

System.out.println();

try{

for(int i=0;ilists.size();i++)

{

//Product product1=(Product)lists.get(i);

//int productId=product1.getProductId();

String tempid=(String)lists.get(i);

tempid=tempid.trim();

int productId=Integer.parseInt(tempid);

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();

product.setProductId(rs.getInt("productId"));

product.setProductCode(rs.getString("productCode"));

product.setProductName(rs.getString("productName"));

product.setUnit(rs.getString("unit"));

product.setPremark(rs.getString("premark"));

all.add(product);

}

}

}finally{

if(prep!=null)

prep.close();

if(prep!=null)

prep.close();

if(conn!=null)

conn.close();

}

return all;

}

(责任编辑:IT教学网)

更多