JDK7集合框架原始碼學習-ArrayList(0)

壹頁書發表於2016-06-22
參考大飛部落格
http://brokendreams.iteye.com/blog/1913619

batchRemove方法
  1. private boolean batchRemove(Collection c, boolean complement) {  
  2.     final Object[] elementData = this.elementData;  
  3.     int r = 0, w = 0;  
  4.     boolean modified = false;  
  5.     try {  
  6.         for (; r < size; r++)  
  7.             if (c.contains(elementData[r]) == complement)  
  8.                 elementData[w++] = elementData[r];  
  9.     } finally {  
  10.         // Preserve behavioral compatibility with AbstractCollection,  
  11.         // even if c.contains() throws.  
  12.         if (r != size) {  
  13.             System.arraycopy(elementData, r,  
  14.                              elementData, w,  
  15.                              size - r);  
  16.             w += size - r;  
  17.         }  
  18.         if (w != size) {  
  19.             // clear to let GC do its work  
  20.             for (int i = w; i < size; i++)  
  21.                 elementData[i] = null;  
  22.             modCount += size - w;  
  23.             size = w;  
  24.             modified = true;  
  25.         }  
  26.     }  
  27.     return modified;  
  28. }  

這個方法是內部方法,就是批次刪除.
加一些除錯資訊.
  1. import java.util.*;  
  2.   
  3. /** 
  4.  * Hello world! 
  5.  */  
  6. public class App {  
  7.     public static void main(String[] args) {  
  8.   
  9.         List l=new ArrayList();  
  10.         l.add(3);  
  11.         l.add(8);  
  12.         batchRemove(l,true);  
  13.     }  
  14.   
  15.     private static boolean batchRemove(Collection c, boolean complement) {  
  16.         final Integer[] elementData = new Integer[]{1,2,3,4,5,6,7,8,9,10};  
  17.         int size=elementData.length;  
  18.         int r = 0, w = 0;  
  19.         boolean modified = false;  
  20.         try {  
  21.             for (; r < size; r++) {  
  22.                 System.out.print("r:"+r+".."+"w:"+w+"..");  
  23.                 for (Integer i : elementData) {  
  24.                     System.out.print("," + i);  
  25.                 }  
  26.                 System.out.print("\n");  
  27.                 if (c.contains(elementData[r]) == complement) {  
  28.                     elementData[w++] = elementData[r];  
  29.                 }  
  30.             }  
  31.         } finally {  
  32.             // Preserve behavioral compatibility with AbstractCollection,  
  33.             // even if c.contains() throws.  
  34.             if (r != size) {  
  35.                 System.arraycopy(elementData, r,  
  36.                         elementData, w,  
  37.                         size - r);  
  38.                 w += size - r;  
  39.             }  
  40.             if (w != size) {  
  41.                 // clear to let GC do its work  
  42.                 for (int i = w; i < size; i++)  
  43.                     elementData[i] = null;  
  44.                 size = w;  
  45.                 modified = true;  
  46.             }  
  47.         }  
  48.         for(Integer i:elementData){  
  49.             System.out.println("Data:"+i);  
  50.         }  
  51.         System.out.println("Size:"+size);  
  52.         return modified;  
  53.     }  
  54. }  

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章