package com.tencent.java.collections;

import java.util.ArrayList;

/**
 * 数组移除操作
 * http://www.importnew.com/20959.html
 * @author ewanzhao
 *
 */
public class ArrayListRemoveOp {
	
	public static void main(String[] args) {
		ArrayList<Integer> arrayList = buildArrayList();
//		testArrayListRemove(arrayList);    //删除角标的方式删除
		
		testArrayListRemoveV2(arrayList);
		
		testArrayListRemoveV3(arrayList);
	}

	private static void testArrayListRemoveV3(ArrayList<Integer> arrayList) {
		
		
	}

	/**
	 * 通过迭代器 remove 元素
	 * result:
	 * before remove:[1, 2, 3, 3, 5, 6, 7, 8, 9, 10]
	Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
	at java.util.ArrayList$Itr.next(Unknown Source)
	at com.tencent.java.collections.ArrayListRemoveOp.testArrayListRemoveV2(ArrayListRemoveOp.java:22)
	at com.tencent.java.collections.ArrayListRemoveOp.main(ArrayListRemoveOp.java:16)
	 * @param arrayList
	 */
	private static void testArrayListRemoveV2(ArrayList<Integer> arrayList) {
		
		System.out.println("before remove:"+arrayList);
		for(Integer integer : arrayList ) {
			if(integer.equals(3)) {
				arrayList.remove(integer);
			}
		}
		
		System.out.println("after remove:"+arrayList);
		
	}

	private static ArrayList<Integer> buildArrayList() {
		ArrayList<Integer> arrayList = new ArrayList<>();
		
		arrayList.add(1);
		arrayList.add(2);
		arrayList.add(3);
		arrayList.add(3);
		arrayList.add(5);
		arrayList.add(6);
		arrayList.add(7);
		arrayList.add(8);
		arrayList.add(9);
		arrayList.add(10);
		return arrayList;
	}

	private static void testArrayListRemove(ArrayList<Integer> arrayList) {
//		arrayList.
		
		System.out.println("before remove:"+arrayList);
		for(int i=0; i< arrayList.size(); i++) {
			if(arrayList.get(i) == 3) {
				arrayList.remove(i);
			}
		}
		
		System.out.println("after remove:"+arrayList);
		
		
	}

}

@@ -59,16 +59,73 @@
 rayList;
+%0Aimport java.util.Collections;%0Aimport java.util.Iterator;
 %0A%0A/**%0A *
@@ -233,16 +233,58 @@
 eOp %7B%0A%09%0A
+%09private static final int EQUAL_NUM = 3;%0A%0A
 %09public 
@@ -368,18 +368,16 @@
 List();%0A
-//
 %09%09testAr
@@ -416,24 +416,26 @@
 %E9%99%A4%E8%A7%92%E6%A0%87%E7%9A%84%E6%96%B9%E5%BC%8F%E5%88%A0%E9%99%A4%0A%09%09%0A
+//
 %09%09testArrayL
@@ -461,16 +461,18 @@
 st);%0A%09%09%0A
+//
 %09%09testAr
@@ -499,24 +499,68 @@
 yList);%0A%09%7D%0A%0A
+%09/**%0A%09 * %E9%80%9A%E8%BF%87%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%9D%A5%E5%88%A0%E9%99%A4%0A%09 * @param arrayList%0A%09 */%0A
 %09private sta
@@ -621,24 +621,327 @@
 yList) %7B%0A%09%09%0A
+%09%09System.out.println(%22before remove:%22+arrayList);%0A%09%09%0A%09%09Iterator%3CInteger%3E iteratorList = arrayList.iterator();%0A%09%09while(iteratorList.hasNext()) %7B%0A%09%09%09Integer data = iteratorList.next();%0A%09%09%09if(data.equals(EQUAL_NUM)) %7B%0A%09%09%09%09iteratorList.remove();%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(%22after remove:%22+arrayList);%0A
 %09%09%0A%09%7D%0A%0A%09/**%0A
@@ -1604,17 +1604,25 @@
 .equals(
-3
+EQUAL_NUM
 )) %7B%0A%09%09%09
@@ -2128,25 +2128,8 @@
 ) %7B%0A
-//%09%09arrayList.%0A%09%09
 %0A%09%09S
@@ -2171,24 +2171,145 @@
 arrayList);%0A
+//%09%09for(int i=0; i%3C arrayList.size(); i++) %7B%0A//%09%09%09if(arrayList.get(i) == 3) %7B%0A//%09%09%09%09arrayList.remove(i);%0A//%09%09%09%7D%0A//%09%09%7D%0A%09%09%0A
 %09%09for(int i=
@@ -2395,16 +2395,26 @@
 ove(i);%0A
+%09%09%09%09i--;%0A%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2461,25 +2461,24 @@
 +arrayList);
-%0A
 %09%09%0A%09%09%0A%09%7D%0A%0A%7D%0A

add 操作数组删除元素