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

Source: Internet
Author: User

This abstract class implements the list interface, inherits the Abstractcollection abstract class, and is the direct parent class of the ArrayList.

Public abstract class Abstractlist<e> extends abstractcollection<e> implements List<e>

This is the basic case of this class.

Protected Abstractlist () {
}

This is the construction method.

Public boolean Add (E e) {
Add (Size (), e);
return true;
}

public void Add (int index, E element) {
throw new Unsupportedoperationexception ();
}

Together, the method calls the second method, passing in the return value of the size () method as the first argument, and the size () method is the abstract method of the parent class, which is not specifically implemented. The second method throws an exception directly here.

Abstract public E get (int index);

Gets the method, abstract method, of the specified position element.

Public E Set (int index, E element) {
throw new Unsupportedoperationexception ();
}

The method that sets the value for the specified position, where the exception is thrown directly.

Public E Remove (int index) {
throw new Unsupportedoperationexception ();
}

The method of deleting the element, where the exception is thrown directly.

public int indexOf (Object o) {
Listiterator<e> it = Listiterator ();
if (o==null) {
while (It.hasnext ())
if (It.next () ==null)
return It.previousindex ();
} else {
while (It.hasnext ())
if (O.equals (It.next ()))
return It.previousindex ();
}
return-1;
}

This method is the method that returns the subscript of the parameter element.

First, get the list iterator. If the argument is empty, the iteration element is started, and when an empty element is found, it returns the subscript of the iterator's just iteration. If the parameter is not empty, the iteration is also started, and when the element and parameter are found to be equal, the subscript of the iterator's just iteration is returned. If there is always no return value, then 1 is returned.

public int lastIndexOf (Object o) {
Listiterator<e> it = listiterator (size ());
if (o==null) {
while (It.hasprevious ())
if (it.previous () ==null)
return It.nextindex ();
} else {
while (It.hasprevious ())
if (O.equals (It.previous ()))
return It.nextindex ();
}
return-1;
}

This method is the method that returns the index of the last occurrence of the parameter element.

First gets the list iterator, and the position of the iteration is the last element. First, if the argument is empty, and if the argument is empty, the list iterator starts the forward iteration and finds the last empty argument, returning the subscript of the iterator's just iteration. If the argument is not empty and the same start iteration is determined, and the last element equal to the parameter is judged, the iterator returns the subscript of the just iteration.

If there is always no return value, 1 is returned.

public void Clear () {
RemoveRange (0, size ());
}

protected void RemoveRange (int fromIndex, int toindex) {
Listiterator<e> it = Listiterator (FromIndex);
for (int i=0, n=toindex-fromindex; i<n; i++) {
It.next ();
It.remove ();
}
}

These two methods look together, the first method is to empty all elements of the method, called the second method, passed 0 and the length of the list of two parameters.

The second method first gets the list iterator, starting with the first one, and if it is the first method call, it is 0.

Go to the For loop, loop the difference of two parameters, begin to delete the element, delete the difference of two parameters, because the iterator starts from the first parameter.



Learn Java source code together--Data structure--abstractlist abstract class (i)

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.