Java and mode Learning Series-iteration submodel

Source: Internet
Author: User
Tags concurrentmodificationexception

 

I, Structure of the iteration sub-(iterator) Mode

The iteration submode can sequentially access an element in the aggregation without exposing the internal representation of the aggregation.

As shown in:

Iterations can be divided into intrinsic iterations and intrinsic iterations.

The external iteration sub-function is suitable for white box aggregation (white box aggregation is to provide external users with aggregation to access their internal element interfaces). Because the iteration logic is provided by the clustering object itself, therefore, such an extra iteration subrole usually only maintains the cursor position of the iteration. Therefore, a specific iteration sub-role is an external class. Its constructor accepts a specific aggregation object and can call the iteration logic of this aggregation object.

The intrinsic iteration sub-applies to Black Box clustering (Black Box clustering does not provide external interfaces to traverse their own element objects), because the element objects of Black Box clustering can only be accessed by internal members of the aggregation, therefore, inner iterations can only be member subclasses within the aggregation.

II, Application of iteration subpattern in Java

In JavaApplication in aggregation

The iterator () Factory method provided by the java. util. Collection interface returns an iterator-type object. The iterator interface is implemented by itr, an internal Member class of the abstractlist class of the collection interface. Therefore, itr is a subclass of inner iteration, but javasactlist also provides its own Traversal method, so it is not a black box clustering, but a white box clustering. The Code is as follows:

Import
Java. util. iterator;

Public
Interface itr extends iterator {

// The metric used to call the next () method again

Int cursor = 0;

// Metric used for the last call

Int lastret =-1;

Int expectedmodcount = modcount;

Public Boolean hasnext (){

Return cursor! = Size ();

}

Public object next (){

Try {

Object next =
Get (cursor );

Checkforcomodification ();

Lastret = cursor ++;

Return next;

} Catch (indexoutofboundsexception
E ){

Checkforcomodification ();

Throw new
Nosuchelementexception ();

}

}

// Delete the last element that has been traversed. The remove () method can only Delete the last element that has been traversed.

Public void remove (){

If (lastret =-1)

Throw new
Illegalstateexception ();

Checkforcomodification ();

Try {

Abstractlist. This. Remove (lastret );

If (lastret <cursor)

Cursor --;

Lastret =-1;

Expectedmodcount =
Modcount;

} Catch (indexoutofboundsexception
E ){

Throw new
Concurrentmodificationexception ();

}

}

Public void checkforcomodification (){

If (modcount! = Expectedmodcount)

Throw new
Concurrentmodificationexception ();

}

}

Among them, variables and methods such as modcount and get (cursor) are owned by the javasactlist class and can be directly used by itr. The checkforcomodification () method checks whether the aggregated content has been directly modified by the outside world (not modified by the remove () method provided by the iterator ). If the aggregated content is directly modified after the iteration subobject is bypassed by the outside, this method immediately throws an exception.

In addition, the javasactlist class also provides the listiterator () method and returns a listitr instance of the class that implements the listiterator interface. The listiterator interface implements Forward Iteration and reverse iteration. It also provides a method to safely modify the column content during the iteration process.

EnumerationAnd iteratorDifferences:(1) Enumeration no Remove Method (2) enumeration is implemented by an unknown class in the element () method in the vector. It does not pay fail fast, that is, in the iteration process, if the aggregate object is accidentally modified, the iteration process immediately captures any exceptions.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.