ArrayList削除要素問題まとめ

1697 ワード

	/**
	 * List remove     ,           ,      i=i+1,              。
	 * 
	 * @author zw
	 * @date  2013-4-15
	 * @return void
	 */
	@Test
	public void testRemove(){
		List al = new ArrayList(5);
		al.add("not1");
		al.add("not2");
		al.add("not3");
		al.add("not4");
		al.add("not5");
		List al2 = new ArrayList();
		List al3 = new ArrayList();
		List al4 = new ArrayList();
		al2.addAll(al);
		al3.addAll(al);
		al4.addAll(al);
		
		 //  ,    remove
		System.out.println("begin al size: "+al.size());
		for(int i=0;i iterator=al2.iterator();
		while(iterator.hasNext()){
			String str=iterator.next();
			if(str.indexOf("not")!=-1){
				iterator.remove();
			}
		}
		System.out.println("end al2 size: "+al2.size());
		Assert.assertEquals(0, al2.size());
		
		//  2:     ,ok
		System.out.println("begin al3 size: "+al3.size());
		for(int i=al3.size()-1;i>=0;i--){   
			String str=al3.get(i);
			if(str.indexOf("not")!=-1){
				al3.remove(i);
			}
		}
		
		System.out.println("end al3 size: "+al3.size());
		Assert.assertEquals(0, al3.size());
		
		//  3:           i   
		System.out.println("begin al4 size: "+al4.size());
		for(int i=0;i