美文网首页java基础Android技术知识Android知识
Java集合系列-ArrayList源码分析(2)

Java集合系列-ArrayList源码分析(2)

作者: 伪文艺大叔 | 来源:发表于2016-11-02 11:12 被阅读40次

在上一篇文章中我们分析了ArrayList的构造方法和添加方法;
http://www.jianshu.com/p/14697d9892b6
这篇文章让我们来看看它的移除方法。

ArrayList 总共有3个移除方法
  • 移除指定位置的数据
public E remove(int index) {    
     Object[] a = array;    
     int s = size;    
     if (index >= s) {        
        throwIndexOutOfBoundsException(index, s);    
     }    
     @SuppressWarnings("unchecked") E result = (E) a[index];     
     System.arraycopy(a, index + 1, a, index, --s - index);    
     a[s] = null;  // Prevent memory leak    
     size = s;    
     modCount++;    
     return result;
}

首先如果index >= s,就会报我们经常会碰到的数组越界异常;
下面的代码就是把index之后的所有数据向前移动一位,然后把最后一位的数据设置为null;

  • 移除集合中的指定数据
 public boolean remove(Object object) {    
    Object[] a = array;    
    int s = size;    
    if (object != null) {        
         for (int i = 0; i < s; i++) {            
             if (object.equals(a[i])) {                
                 System.arraycopy(a, i + 1, a, i, --s - i);                
                 a[s] = null;  // Prevent memory leak                
                 size = s;                
                 modCount++;                
                 return true;            
             }        
         }    
   } else {        
        for (int i = 0; i < s; i++) {            
            if (a[i] == null) {                
                System.arraycopy(a, i + 1, a, i, --s - i);                
                a[s] = null;  // Prevent memory leak                
                size = s;                
                modCount++;                
                return true;            
            }        
        }    
   }    
   return false;
}

从第三行的逻辑开始看,首先判断要移除的对象是否为空;
如果不为空,循环整个数组,找到object数组所在的位置,然后逻辑就跟上面的移除类型,把object在数组中所在位置的后面的数据向前移动一位,并且设置最后一位为null。

如果为空,就是找到null所在的位置,和上面的逻辑一致。
从这段代码可以看出,ArrayList是允许添加null数据的,在移除的时候移除null数据,是移除最前面的null数据,找到就return。

  • 移除集合中指定集合的数据
public boolean removeAll(Collection<?> collection) {    
    boolean result = false;    
    Iterator<?> it = iterator();    
    while (it.hasNext()) {        
      if (collection.contains(it.next())) {            
        it.remove();            
        result = true;        
      }    
    }    
    return result;
}

这个移除方法并不是ArrayList自己本身的,它是AbstractCollection类的,那ArrayList和它是什么关系呢?ArrayList的父类是AbstractList,而AbstractList的父类是AbstractCollection,所以ArrayList也是AbstractCollection的子类。

这个方法内部实现是通过迭代器来实现的,循环遍历当前的集合,如果遍历得到的数据存在于要删除的collection集合当中,就移除这条数据。

ArrayList其它常用方法
  • 集合大小
@Override 
public int size() {    
      return size;
}

就是返回标示数量的size字段

  • 清空方法
@Override 
public void clear() {    
    if (size != 0) {        
        Arrays.fill(array, 0, size, null);        
        size = 0;        
        modCount++;    
     }
}

就是把数组所有项都置null

  • 包含方法
public boolean contains(Object object) {    
     Object[] a = array;    
     int s = size;    
     if (object != null) {        
       for (int i = 0; i < s; i++) {           
         if (object.equals(a[i])) {                
             return true;            
         }        
       }    
      } else {        
        for (int i = 0; i < s; i++) {            
           if (a[i] == null) {                
              return true;            
            }       
         }    
      }    
    return false;
}

大致意思就是循环数组,通过equals方法寻找相同的对象,所以要用到这个方法的话要重写对象的equals方法。

  • 转化数组方法
public Object[] toArray() {    
.   int s = size;    
      Object[] result = new Object[s];    
      System.arraycopy(array, 0, result, 0, s);    
     return result;
}

大致意思就是先new一个数组,然后copy数据到新数组。

至此ArrayList的常用的一些方法就分析完了。

相关文章

网友评论

    本文标题:Java集合系列-ArrayList源码分析(2)

    本文链接:https://www.haomeiwen.com/subject/rfsvuttx.html