JDK7集合框架原始碼學習-ArrayList(0)
參考大飛部落格
http://brokendreams.iteye.com/blog/1913619
batchRemove方法
這個方法是內部方法,就是批次刪除.
加一些除錯資訊.
r:0..w:0..,1,2,3,4,5,6,7,8,9,10
r:1..w:0..,1,2,3,4,5,6,7,8,9,10
r:2..w:0..,1,2,3,4,5,6,7,8,9,10
r:3..w:1..,3,2,3,4,5,6,7,8,9,10
r:4..w:1..,3,2,3,4,5,6,7,8,9,10
r:5..w:1..,3,2,3,4,5,6,7,8,9,10
r:6..w:1..,3,2,3,4,5,6,7,8,9,10
r:7..w:1..,3,2,3,4,5,6,7,8,9,10
r:8..w:2..,3,8,3,4,5,6,7,8,9,10
r:9..w:2..,3,8,3,4,5,6,7,8,9,10
Data:3
Data:8
Data:null
Data:null
Data:null
Data:null
Data:null
Data:null
Data:null
Data:null
Size:2
試試將complement改為false
batchRemove(l,false);
r:0..w:0..,1,2,3,4,5,6,7,8,9,10
r:1..w:1..,1,2,3,4,5,6,7,8,9,10
r:2..w:2..,1,2,3,4,5,6,7,8,9,10
r:3..w:2..,1,2,3,4,5,6,7,8,9,10
r:4..w:3..,1,2,4,4,5,6,7,8,9,10
r:5..w:4..,1,2,4,5,5,6,7,8,9,10
r:6..w:5..,1,2,4,5,6,6,7,8,9,10
r:7..w:6..,1,2,4,5,6,7,7,8,9,10
r:8..w:6..,1,2,4,5,6,7,7,8,9,10
r:9..w:7..,1,2,4,5,6,7,9,8,9,10
Data:1
Data:2
Data:4
Data:5
Data:6
Data:7
Data:9
Data:10
Data:null
Data:null
Size:8
http://brokendreams.iteye.com/blog/1913619
batchRemove方法
- private boolean batchRemove(Collection c, boolean complement) {
- final Object[] elementData = this.elementData;
- int r = 0, w = 0;
- boolean modified = false;
- try {
- for (; r < size; r++)
- if (c.contains(elementData[r]) == complement)
- elementData[w++] = elementData[r];
- } finally {
- // Preserve behavioral compatibility with AbstractCollection,
- // even if c.contains() throws.
- if (r != size) {
- System.arraycopy(elementData, r,
- elementData, w,
- size - r);
- w += size - r;
- }
- if (w != size) {
- // clear to let GC do its work
- for (int i = w; i < size; i++)
- elementData[i] = null;
- modCount += size - w;
- size = w;
- modified = true;
- }
- }
- return modified;
- }
這個方法是內部方法,就是批次刪除.
加一些除錯資訊.
- import java.util.*;
- /**
- * Hello world!
- */
- public class App {
- public static void main(String[] args) {
- List l=new ArrayList();
- l.add(3);
- l.add(8);
- batchRemove(l,true);
- }
- private static boolean batchRemove(Collection c, boolean complement) {
- final Integer[] elementData = new Integer[]{1,2,3,4,5,6,7,8,9,10};
- int size=elementData.length;
- int r = 0, w = 0;
- boolean modified = false;
- try {
- for (; r < size; r++) {
- System.out.print("r:"+r+".."+"w:"+w+"..");
- for (Integer i : elementData) {
- System.out.print("," + i);
- }
- System.out.print("\n");
- if (c.contains(elementData[r]) == complement) {
- elementData[w++] = elementData[r];
- }
- }
- } finally {
- // Preserve behavioral compatibility with AbstractCollection,
- // even if c.contains() throws.
- if (r != size) {
- System.arraycopy(elementData, r,
- elementData, w,
- size - r);
- w += size - r;
- }
- if (w != size) {
- // clear to let GC do its work
- for (int i = w; i < size; i++)
- elementData[i] = null;
- size = w;
- modified = true;
- }
- }
- for(Integer i:elementData){
- System.out.println("Data:"+i);
- }
- System.out.println("Size:"+size);
- return modified;
- }
- }
r:0..w:0..,1,2,3,4,5,6,7,8,9,10
r:1..w:0..,1,2,3,4,5,6,7,8,9,10
r:2..w:0..,1,2,3,4,5,6,7,8,9,10
r:3..w:1..,3,2,3,4,5,6,7,8,9,10
r:4..w:1..,3,2,3,4,5,6,7,8,9,10
r:5..w:1..,3,2,3,4,5,6,7,8,9,10
r:6..w:1..,3,2,3,4,5,6,7,8,9,10
r:7..w:1..,3,2,3,4,5,6,7,8,9,10
r:8..w:2..,3,8,3,4,5,6,7,8,9,10
r:9..w:2..,3,8,3,4,5,6,7,8,9,10
Data:3
Data:8
Data:null
Data:null
Data:null
Data:null
Data:null
Data:null
Data:null
Data:null
Size:2
試試將complement改為false
batchRemove(l,false);
r:0..w:0..,1,2,3,4,5,6,7,8,9,10
r:1..w:1..,1,2,3,4,5,6,7,8,9,10
r:2..w:2..,1,2,3,4,5,6,7,8,9,10
r:3..w:2..,1,2,3,4,5,6,7,8,9,10
r:4..w:3..,1,2,4,4,5,6,7,8,9,10
r:5..w:4..,1,2,4,5,5,6,7,8,9,10
r:6..w:5..,1,2,4,5,6,6,7,8,9,10
r:7..w:6..,1,2,4,5,6,7,7,8,9,10
r:8..w:6..,1,2,4,5,6,7,7,8,9,10
r:9..w:7..,1,2,4,5,6,7,9,8,9,10
Data:1
Data:2
Data:4
Data:5
Data:6
Data:7
Data:9
Data:10
Data:null
Data:null
Size:8
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2120733/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JDK7集合框架原始碼學習-ArrayList(1)JDK框架原始碼
- JDK7集合框架原始碼學習-ArrayList(4)SubListJDK框架原始碼
- JDK7集合框架原始碼學習-ArrayList(3)迭代器JDK框架原始碼
- JDK7集合框架原始碼學習-ArrayList(2)迭代器JDK框架原始碼
- 集合框架原始碼學習之ArrayList框架原始碼
- JDK7集合框架原始碼學習-ArrayDequeJDK框架原始碼
- Java集合原始碼學習(2)ArrayListJava原始碼
- Java 集合框架------ArrayList原始碼分析Java框架原始碼
- Java 集合框架 ArrayList 原始碼剖析Java框架原始碼
- 集合框架-ArrayList集合的toString()方法原始碼解析框架原始碼
- 集合-ArrayList 原始碼解析原始碼
- 集合框架原始碼學習之LinkedList框架原始碼
- 【集合框架】JDK1.8原始碼分析之ArrayList(六)框架JDK原始碼
- 【Java集合】ArrayList原始碼分析Java原始碼
- JAVA集合:ArrayList原始碼分析Java原始碼
- 集合原始碼分析[3]-ArrayList 原始碼分析原始碼
- Java集合原始碼剖析——ArrayList原始碼剖析Java原始碼
- 【Java集合原始碼剖析】ArrayList原始碼剖析Java原始碼
- Java容器原始碼學習--ArrayList原始碼分析Java原始碼
- 集合框架原始碼學習之HashMap(JDK1.8)框架原始碼HashMapJDK
- Java集合之ArrayList原始碼解析Java原始碼
- java集合原始碼分析(三):ArrayListJava原始碼
- JAVA ArrayList集合底層原始碼分析Java原始碼
- Java集合乾貨——ArrayList原始碼分析Java原始碼
- 集合框架2- ArrayList框架
- Java集合框架(一)-ArrayListJava框架
- Java 集合框架(二)—— ArrayListJava框架
- java集合學習(一):詳解ArrayListJava
- 死磕 java集合之ArrayList原始碼分析Java原始碼
- Java集合-ArrayList原始碼解析-JDK1.8Java原始碼JDK
- Java集合乾貨1——ArrayList原始碼分析Java原始碼
- 純手寫Arraylist集合框架框架
- Java 集合 ArrayList 原始碼分析(帶著問題看原始碼)Java原始碼
- Java集合原始碼學習(1)介面Java原始碼
- Java集合原始碼學習(4)HashSetJava原始碼
- Java集合原始碼學習(5)HashMapJava原始碼HashMap
- Okio 框架原始碼學習框架原始碼
- 【Java集合原始碼剖析】Java集合框架Java原始碼框架