Source code analysis of Vector traversal elements based on java construction method

  • 2021-11-13 01:27:32
  • OfStack

(Note: This article is based on JDK 1.8)

Preface

After any container class object is used to hold elements, it is always necessary to traverse elements, that is, to access each element once one by one. In addition to the conventional subscript that depends on array objects, the encapsulated iterator is more commonly used to traverse elements. Today, let's learn how the iterator in Vector is designed. The methods related to iterators are:

iterator()

listIterator()

listIterator(int index)

All 3 methods defined in Vector will return 1 iterator object … Briefly talk about the origin of these 3 methods

The origin of iterator () method

iterator () method, Vector implementation of Iterable interface specification method, how you intuitively to see the class structure of Vector, you can not find the definition of Iterable interface, you should see this:


public class Vector<E>
    extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
       ...... omit the code...... 
}

So where is the Iterable interface? Yes, in the List interface, the List interface inherits the Collection interface, and the Collection interface inherits the Iterable interface. Because Vector implements the List interface, the implementation requirements of the iterator () method are indirectly obtained.

The List interface represents linear table capability, the Collection interface represents set capability, and the Iterable interface represents traversal capability

List extends the capabilities of Collection, while Collection extends the capabilities of Iterable. List has the greatest capabilities and conforms to such specifications. Since it is a linear table, it should of course have the capabilities defined in Collection, while since it is a set, it should have the capabilities of traversing elements.

The Origin of listIterator () and listIterator (int) Methods

These two methods are derived from the definition in AbstractList, the parent class of Vector, but Vector rewrites them, while AbstractList implements these two methods according to its own List interface. Not many words, let's take a look next

Analysis of iterator () Method


    public synchronized Iterator<E> iterator() {
        return new Itr();
    }

Method used to return 1 iterator object, synchronized decorated, that can only be executed by the thread that acquired the lock on the object

1. Create an Itr object

Itr is a common inner class defined in Vector, which produces objects used to represent iterators, also known as iterator objects

2. Return the iterator object to the caller

Analysis of listIterator () Method


    public synchronized ListIterator<E> listIterator() {
        return new ListItr(0);
    }

A method used to return an iterator object that is used to traverse from the first element

1. Create an ListItr object

ListItr is also a common inner class defined in Vecor, and its constructor can pass in 1 parameter representing the starting subscript (from which element)

2. Return the ListItr object to the caller

Analysis of listIterator (int) Method


    public synchronized ListIterator<E> listIterator(int index) {
        if (index < 0 || index > elementCount)
            throw new IndexOutOfBoundsException("Index: "+index);
        return new ListItr(index);
    }

Used to return 1 iterator object, passing in parameters indicating which element this iterator can start from

1. Check whether the subscript passed in is reasonable

When the incoming subscript is less than 0, or the incoming subscript is greater than elementCount (that is, the total number of elements and the subscript of new elements to be added), it is unreasonable, and the IndexOutOfBoundsException () object is thrown here

2. Create an ListItr object

Using the subscript passed in, pass it into the constructor of ListItr, and create an iterator object that specifies starting at a subscript

3. Return the iterator object ListItr

Summarize

Iterator operation, all rely on Itr class, and ListItr class generated by the object, the real iterator object is them, so the next 1 will directly analyze, Itr class and ListItr class all methods

The above is based on java construction method Vector traversal element source code analysis details, more about java construction method Vector information please pay attention to other related articles on this site!


Related articles: