Common learning Java source code--Data structure--abstractlist abstract class (V)

Source: Internet
Author: User
Tags concurrentmodificationexception

Private class Listitr extends Itr implements Listiterator<e>

This is again an inner class. Inherits from the previous inner class, implements the Listiterator interface, which is an iterator specifically iterating the list,

LISTITR (int index) {
cursor = index;
}
The first is the constructor of the default modifier decoration, which assigns the parameter to the cursor variable.

public Boolean hasprevious () {
return cursor! = 0;
}
This method determines whether a forward iteration is possible, and the basis of the judgment is whether the cursor is 0.

Public E Previous () {
Checkforcomodification ();
try {
int i = cursor-1;
E previous = Get (i);
Lastret = cursor = i;
return previous;
} catch (Indexoutofboundsexception e) {
Checkforcomodification ();
throw new Nosuchelementexception ();
}
}

This method returns the previous element. First, determine whether the two Modcount values are equal.

Then create the variable i assignment to the cursor minus one, call the Get method to get the previous element, assign both Lastret and cursor to I, and finally return to the previous element. If this procedure is an exception, first determine whether two modcount are synchronized, and then throw an exception.

public int Nextindex () {
return cursor;
}
This method gets the next element subscript, which is the cursor value.

public int Previousindex () {
return cursor-1;
}

This method gets the previous element subscript, which is the cursor value minus 1.
public void Set (E e) {
if (Lastret < 0)
throw new IllegalStateException ();
Checkforcomodification ();


try {
AbstractList.this.set (Lastret, E);
Expectedmodcount = Modcount;
} catch (Indexoutofboundsexception ex) {
throw new Concurrentmodificationexception ();
}
}

This method is the method of assigning values first to determine whether the Lastret element is less than 0, or throw an exception, less than 0 means the element has just been deleted, has not been iterated. Then check if two modcount are equal.

Then call the Set method assignment, assign a value to the Lastret subscript, and then synchronize the two modcount.

public void Add (e e) {
Checkforcomodification ();
try {
int i = cursor;
AbstractList.this.add (i, E);
Lastret =-1;
cursor = i + 1;
Expectedmodcount = Modcount;
} catch (Indexoutofboundsexception ex) {
throw new Concurrentmodificationexception ();
}

}

This method is the method of adding elements. First check that two modcount variables are equal.

Then create the variable I to assign to the cursor. The Add method is then called, adding the subscript is I, then Lastret assigns the value -1,cursor to I+1, and the last two Modcount is synchronized.

Throws an exception if there is an exception to this procedure.

Common learning Java source code--Data structure--abstractlist abstract class (V)

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.