Java Iterative sub-mode detailed _java

Source: Internet
Author: User
Tags int size stub concurrentmodificationexception

The structure of the

iterator (iterator) pattern:
An iterative child pattern can sequentially access elements in a cluster without exposing the internal representation of the aggregation.
iterations can be divided into external iteration Sub and intrinsic iteration Sub .
External Iteration Child: The is suitable for white box aggregation (white box aggregation is to provide external access to the aggregation of its own internal element interfaces), because the logic of the iteration is provided by the clustered object itself, so the external iterative child role often only holds the cursor position of the iteration. So the specific iteration child role is an external class whose constructor accepts a concrete aggregate object, so that the iterative logic of the aggregate object can be invoked.
Intrinsic iteration: applies to black box aggregation (black box aggregation does not provide external interface to traverse its own element object), because the element object of the black box aggregation can only be accessed by the aggregated internal member, so the intrinsic iteration can only be a member subclass within the aggregation.
Simple demonstration:

Package test.edu.inter; 
 Public interface Iteratorobj {/** * moves to the first element/public void A (); 
 /** * Move to the next element */public Boolean hasnextitem (); 
 
/** * Return the current element */public Object currentitem (); 
 
} package Test.edu.inter; 
Public interface DataSet {public iteratorobj getiterator (); 
 
 
} package Test.edu.inter; 
 public class Iterator1 implements Iteratorobj {private dataobj set; 
 private int size; 
  
 private int index=0; 
  Public Iterator1 (Dataobj set) {this.set = set; 
 This.size = Set.getsize (); 
 @Override public void A () {//TODO auto-generated method Stub this.index = 0; 
  @Override public boolean Hasnextitem () {if (index<size) {true; 
 return false; 
  @Override public Object CurrentItem () {Object OB = Set.getitem (index); 
  if (index<size) {index++; 
 return OB; 
 
}} package Test.edu.inter; public class Dataobj implements DataSet {priVate object[] Objarray = null; 
  /** * Incoming aggregate object/public Dataobj (object[] objarray) {this.objarray = Objarray; 
 @Override public Iteratorobj Getiterator () {Return to New Iterator1 (this); 
 Public Object getitem (int index) {return objarray[index]; 
 public int GetSize () {return objarray.length; 
 
}} package Test.edu.inter; public class Client {/** * @param args */public static void main (string[] args) {//TODO auto-generated 
  Method stub string[] str={"12312", "Dasda", "DASD", "12d", "ASD"}; 
  Dataobj ao = new dataobj (str); 
  Iteratorobj io = Ao.getiterator (); 
  while (Io.hasnextitem ()) {System.out.println (Io.currentitem ()); 

 } 
 } 
}

Run Result:

12312 
dasda 
dasd 
12d 
ASD 

Content Expansion: application in Java aggregation
the iterator () factory method is provided in the Java.util.Collection interface to return a iterator type object. The collection interface Abstractlist the inner member class of the class ITR implements the iterator interface. So ITR is an intrinsic iterative subclass, but Abstractlist also provides its own traversal method, so it's not a black box aggregation, but a white box aggregation. The code is as follows:

Import Java.util.Iterator;
   the indicator int cursor = 0 that is used by the public interface ITR extends iterator{//re-invoke the next () method;
   The metric used for the most recent 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 to Next;
       }catch (indexoutofboundsexception e) {checkforcomodification ();
     throw new Nosuchelementexception (); }//Delete the last traversed element, the remove () method can only delete the last traversed element public void remove () {if (Lastret ==-1) throw new Illegalstatee
     Xception ();
     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 (); }
}

The Modcount, get (cursor) and other variables and methods are all owned by the Abstractlist class, ITR can be used directly. The method Checkforcomodification () checks whether the contents of the aggregate have just been modified directly by the outside world (not through the Remove () method provided by the iteration child). This method throws an exception immediately if the contents of the aggregation are modified by the outside world by bypassing the iteration child after the iteration begins.
In addition: The Abstractlist class also provides a Listiterator () method that returns a class Listitr instance that implements the Listiterator interface. The Listiterator interface implements forward and backward iterations, while also providing a way to safely modify the contents of columns during an iterative process.
The difference between enumeration and iterator: (1) enumeration No Remove method (2) enumeration is implemented by an unnamed class in the element () method of the vector, it does not pay fail Fast, That is, in an iterative process, a clustered object is accidentally modified directly by the outside world, and the iterative process catches any exceptions immediately.

The above is the entire content of this article, I hope to help you learn.

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.