In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use source code to analyze Vector, I believe that many inexperienced people are at a loss about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Introduction to Vector public class Vector extends AbstractList implements List, RandomAccess, Cloneable, java.io.Serializable {}
Vector inherits AbstractList and implements List;, so it is a queue that supports operations such as add, delete, modify, traversal, etc.
Vector implements the RandomAccess interface and supports fast random access policies.
Vector implements the Cloneable interface and overrides the clone method, so it can be cloned.
Vector implements the Serializable interface, so it can be serialized.
The operation of Vector is thread safe
The member variable / * * maximum capacity of the collection * * / private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8 role savings * holds the number of elements added to the Vector * / protected Object [] the number of elements in the elementData;/** collection * * / protected int elementCount;/** set growth factor * / protected int capacityIncrement Constructor / * * default constructor with initialization capacity of 10 * * / public Vector () {this (10);} public Vector (int initialCapacity) {this (initialCapacity, 0);} public Vector (int initialCapacity, int capacityIncrement) {if (capacityIncrement)
< 0) { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement;}/** 通过集合初始化Vector **/public Vector(Collection c)、retainAll(Collection c)、removeAllElements() remove(int index) public synchronized E remove(int index) { //判断index是否越界 if (index < 0 || index >= elementCount) throw new ArrayIndexOutOfBoundsException (index + ">" + elementCount); E oldValue = elementData (index); int needMoved = elementCount-index-1; if (needMoved > 0) {/ / move the elements after index forward System.arraycopy (elementData, index + 1, elementData, index, needMoved);} elementData [--elementCount] = null; modCount + +; return oldValue } / * * method for finding elements * * / E elementData (int index) {return (E) elementData [index];}
Remove (Object o)
/ / remove elements and synchronize threads through removeElement public boolean remove (Object o) {return removeElement (o);}
RemoveElement (Object obj)
/ * * if the element is found by indexOf method, remove it by removeElementAt method and return false * * / public synchronized boolean removeElement (Object obj) {modCount + +; int I = indexOf (obj); if (I > 0) {removeElementAt (I); return true;} return false;} @ Overridepublic int indexOf (Object o) {return indexOf (o, 0) } public synchronized int indexOf (Object o, int index) {if (o = = null) {for (int I = index; I)
< elementCount ; i++) if (elementData[i]==null) return i; } else { for (int i = index ; i < elementCount ; i++) if (o.equals(elementData[i])) return i; } return -1;} removeElementAt(int index) /** 同上方的remove(int index)方法基本一致 无返回值 **/public synchronized void removeElementAt(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException (index + ">" + elementCount); else if (index
< 0) throw new ArrayIndexOutOfBoundsException(index); int needMoved = elementCount - index - 1; if (needMoved >0) {/ / move the elements after index forward System.arraycopy (elementData, index + 1, elementData, index, needMoved);} elementData [--elementCount] = null; modCount + +;}
RemoveAll (Collection c)
/ * * / public synchronized boolean removeAll (Collection c) {return super.removeAll (c);} / * * AbstractCollection removeAll**/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;} / * * through the retainAll of the parent class AbstractCollection * * / public synchronized boolean retainAll (Collection c) {return super.retainAll (c);} / * * retainAll**/public boolean retainAll (Collection c) {Objects.requireNonNull (c) of AbstractCollection; boolean modified = false; Iterator it = iterator () While (it.hasNext ()) {if (! c.contains (it.next () {it.remove (); modified = true;}} return modified;}
RemoveAllElements ()
/ * * remove all elements * * / public synchronized void removeAllElements () {for (int I = 0; I
< elementCount; i++) { elementData[i] = null; } elementCount = 0; modCount ++;}元素查找 查找元素主要有get(int index)、elementAt(int index),两者方法都通过上方的elementData(int index)方法实现 get(int index) public E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException (index); return elementData (index);} element update
The update elements mainly have two methods: set (int index, E element) and setElementAt (E obj, int index). The main reason is that the return values are different.
Set (int index, E element)
Public synchronized E set (int index, E element) {if (index > = elementCount) throw new ArrayIndexOutOfBoundsException (index); E e = elementData (index); elementData [index] = element; return e;}
SetElementAt (E obj, int index)
Public synchronized void setElementAt (E obj, int index) {if (index > = elementCount) throw new ArrayIndexOutOfBoundsException (index); elementData [index] = obj;} after reading the above, have you mastered how to analyze Vector with source code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.