Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The Construction method of java A case study of adding elements to Vevtor

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the example analysis of adding elements to Vevtor, the construction method of java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "the construction method of java Vevtor to add elements of the case analysis" it!

Preface

Including the add () method of the iterator, there are 7 methods to add elements in Vector, 5 methods to add a single element, 2 methods to add multiple elements, and then analyze their implementation together. Vector is a thread-safe container class, how does its add function make it thread-safe?

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

The method used to add 1 element, modified by synchronized, can only be executed by the thread that acquires the object lock. Other threads that do not acquire the object lock will blocked at the entrance of the method, waiting for the thread that already holds the object lock to release the object lock. The passed parameter is the element object to be added, and the type is the specified type parameter E.

1. Modify the modCount value first

In Vecotor's parent class AbstractList, the instance variable modCount is defined, which means that each Vector object also holds a modCount, where + 1 means that the element held by the current Vector object has changed. This modcount value is designed to prevent users from using container classes in multithreading, often called fail-fast mechanism, but Vector itself is a thread-safe container class, why are you still using modCount to do + + here? It is difficult to understand.

2. Then check whether a new element can be added to the underlying array capacity.

Check by calling the ensureCapacityHelper () method. The input parameter is a value after the total number of actual elements + 1, which is used to confirm whether the capacity of the current array needs to be expanded. If the capacity of the array cannot be added with a new element, the array objects held by the current Vector object will be expanded in this method (the expansion method will be analyzed in a separate article. Just know here that the capacity is not enough, so expand first).

3. Assign the element to a subscript in the array object and increase the instance variable that represents the total number of elements

First use the elmentCount held by the Vector object as the array subscript, assign the newly added elements to the corresponding subscript in the elementData array, and then increase the elementCount that represents the total number of elements actually held by 1. Here the instance variable elementCount plays two roles at the same time, one is used to record the total number of elements actually held by the Vector object, and the other is used to hold the subscript of the underlying array object as the Vector object!

4. Return the result of adding an element

True is returned every time, indicating that the element was added successfully.

Add (int,E) method to analyze public void add (int index, E element) {insertElementAt (element, index);}

Method for adding an element at a specified subscript, the first parameter index represents the specified subscript, and the second parameter element represents the added element

The insertElementAt () method is called in the method body, and the two parameters index and element are passed into the insertElementAt () method at the same time, and the element is added by the insertElementAt () method.

InsertElementAt () method to analyze 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] = obj; elementCount++;}

The method used to add 1 element at the specified subscript. The first parameter obj represents the added element object, and the second parameter index represents the specified subscript. (note: the order of parameters here is really a foreigner's idea.) it is also modified by synchronized. Only the thread that acquires the object lock can execute the method. The thread that does not acquire the object lock is at the method entrance and is in the blocked state.

1. Modify the modCount value first

The instance variable modCount is defined in the parent class AbstractList, which is used to prevent container classes from being used in multithreading, often referred to as fail-fast mechanism I. This value is increased by 1 to indicate that the element held by the Vector object has changed.

2. Check whether the passed subscript value is legal

If the subscript value index passed in is greater than the elementCount value of the total number of elements actually held by the Vector object, the ArrayIndexOutOfBoundsException object is thrown and the user is prompted for "index > elementCount" (replace with the actual value)

3. Check whether capacity expansion is needed.

Call the ensureCapacityHelper () method and pass the value of elementCount+1 in at the same time

4. Copy the array elements to make a free space.

Copy the element through System's static method arraycopy (). The first parameter of arraycopy () is the source array object, the second parameter is the starting subscript of the source array object (from which element to copy), the third parameter is the target array object, the fourth parameter is the starting subscript of the target array object (from which element to paste), and the fifth parameter is the number of elements to be copied! You just need to move a location here to store the elements you are about to insert!

5. Insert an element into the specified location

Free space has been made, just insert the element into the specified subscript of the array

6. The total number of elements increases

The elementCount held by the Vector object increases by 1

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

Also for synchronized modification, the method of adding an element, unlike the add () method, does not return a value. Almost all the same, there is no more redundant analysis here

AddAll () method to analyze public synchronized boolean addAll (Collection)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report