Java容器类源码-Vector的最全的源码分析(三)

 

Java容器类源码-Vector的最全的源码分析(三)

 

(16) public synchronized void removeElementAt(int index)

源码解释:

获取到index位置后有多少个元素,并将index位置后面的元素复制到index位置前的后面,再将index位置置空。复制操作通过JNI实现。

 public synchronized void removeElementAt(int index) {
 modCount++;
 if (index >= elementCount) {
 throw new ArrayIndexOutOfBoundsException(index + " >= " +
 elementCount);
 }
 else if (index < 0) {
 throw new ArrayIndexOutOfBoundsException(index);
 }
 int j = elementCount - index - 1;		// 获取第index位置后有多少个元素
 if (j > 0) {
 System.arraycopy(elementData, index + 1, elementData, index, j);	// 将emlementData[j, elementCount]复制到elementData[0, index]的后面
 }
 elementCount--;
 elementData[elementCount] = null; // 等待GC
 }

(17) public synchronized void insertElementAt(E obj, int index)

源码解释:

将index位置后的元素复制到原数组的index+1位置后的地方,并在index位置赋值obj。

 public synchronized void insertElementAt(E obj, int index) {
 modCount++;
 if (index > elementCount) {
 throw new ArrayIndexOutOfBoundsException(index
 + " > " + elementCount);
 }
 ensureCapacityHelper(elementCount + 1);		// 判断是否需要扩容
 System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);	
// 将elementData[index+1, elementCount]复制到elementData[index+1, elementCount +1]位置,
 elementData[index] = obj;	// 在index位置赋值obj
 elementCount++;
 }

(18) public synchronized void addElement(E obj)

源码解释:

在数组最后添加数据obj。先扩容,再通过数组赋值。

 public synchronized void addElement(E obj) {
 modCount++;
 ensureCapacityHelper(elementCount + 1);
 elementData[elementCount++] = obj;
 }

(19) public synchronized boolean removeElement(Object obj)

源码解释:

移除元素。先获取到移除元素在数组的位置,然后调用removeElementAt方法移除元素。

 public synchronized boolean removeElement(Object obj) {
 modCount++;
 int i = indexOf(obj);
 if (i >= 0) {
 removeElementAt(i);
 return true;
 }
 return false;
 }

(20) public synchronized void removeAllElements()

源码解释:

移除所有元素。即将所有元素置为null,等待gc。

 public synchronized void removeAllElements() {
 modCount++;
 // Let gc do its work
 for (int i = 0; i < elementCount; i++)
 elementData[i] = null;
 
 elementCount = 0;
 }

(21) public synchronized E get(int index)

源码解释:

获取index位置的元素。

 public synchronized E get(int index) {
 if (index >= elementCount)
 throw new ArrayIndexOutOfBoundsException(index);
 
 return elementData(index);
 }

(22) public synchronized E set(int index, E element)

源码解释:

修改index位置的值为element。实现原理也是直接数组赋值。

 public synchronized E set(int index, E element) {
 if (index >= elementCount)
 throw new ArrayIndexOutOfBoundsException(index);
 
 E oldValue = elementData(index);
 elementData[index] = element;
 return oldValue;
 }

(23) public synchronized boolean add(E e)

源码解释:

在最后位置新增元素e。先判断是否需要扩容,然后赋值。实现原理和addElement是一样的。

 public synchronized boolean add(E e) {
 modCount++;
 ensureCapacityHelper(elementCount + 1);
 elementData[elementCount++] = e;
 return true;
 }

(24) public boolean remove(Object o)

源码解释:

移除元素,和removeElement方法一样。

public boolean remove(Object o) {

return removeElement(o);

}

(25) public void add(int index, E element)

源码解释:

添加元素,和insertElementAt方法一样。

 public void add(int index, E element) {
 insertElementAt(element, index);
 }

(26) public synchronized E remove(int index)

源码解释:

移除index位置的元素。实现思路和removeElementAt类似。

 public synchronized E remove(int index) {
 modCount++;
 if (index >= elementCount)
 throw new ArrayIndexOutOfBoundsException(index);
 E oldValue = elementData(index);	// 获取到旧值
 
 int numMoved = elementCount - index - 1;	// index位置后面元素的个数
 if (numMoved > 0)
 System.arraycopy(elementData, index+1, elementData, index, numMoved);	
// 将elementData[index+1, elementCount]移到element[0, index]后
 elementData[--elementCount] = null; // Let gc do its work
 
 return oldValue;
 }

(27) public void clear()

源码解释:

移除所有元素,直接调用removeAllElements()。

 public void clear() {
 removeAllElements();
 }

(28) public synchronized boolean containsAll(Collection<?> c)

源码解释:

判断是否包含集合c的所有元素。调用AbstractCollection的实现,代码也很简单,不赘叙。

 public synchronized boolean containsAll(Collection<?> c) {
 return super.containsAll(c);
 }
 public boolean containsAll(Collection<?> c) {
 for (Object e : c)
 if (!contains(e))
 return false;
 return true;
 }

(29) public synchronized boolean addAll(Collection<? extends E> c)

源码解释:

把集合c复制到当前数组的末尾。先判断是否需要扩容,然后调用JNI的arraycopy实现复制。

 public synchronized boolean addAll(Collection<? extends E> c) {
 modCount++;
Object[] a = c.toArray(); 
int numNew = a.length; 
ensureCapacityHelper(elementCount + numNew);
 System.arraycopy(a, 0, elementData, elementCount, numNew); 
elementCount += numNew; 
return numNew != 0; 
}

(30) public synchronized boolean removeAll(Collection<?> c)

源码解释:

将包含集合c的所有元素移除。调用AbstractCollection的实现,代码也很简单,不赘叙。

public boolean removeAll(Collection<?> c) {
 Objects.requireNonNull(c);
 boolean modified = false;
 Iterator<?> it = iterator();
 while (it.hasNext()) {
 if (c.contains(it.next())) {
 it.remove();
 modified = true;
 }
 }
 return modified;
 }

每天都在分享文章,也每天都有人想要我出来给大家分享下怎么去学习Java。大家都知道,我们是学Java全栈的,大家就肯定以为我有全套的Java系统教程。没错,我是有Java全套系统教程,进扣裙【47】974【9726】所示,进群的时候记得表明自己想要学习什么,不要用小号,这样小编才好给你们发定向资源,今天小编就免费送!~

Java容器类源码-Vector的最全的源码分析(三)

 

“我们相信人人都可以成为一个程序员,现在开始,找个师兄,带你入门,学习的路上不再迷茫。这里是ja+va修真院,初学者转行到互联网行业的聚集地。"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值