Java-preliminary Understanding-Common Object API (Collection framework-listiterator interface)

Source: Internet
Author: User

One.

Take the list out of the way and say it, (it is possible to take the action of removing the element from the list).

Take all the elements out of the list, using iterator, because list belongs to the Colletion subclass, and the list of things Colletion has. (The list above is just an empty container and then constantly adding elements to it, or deleting elements) returned to the iterator how is that understood ?

DOS results without any problems.

List has its own unique element to take out the way, List.get (index), enter a set of the value of a corner, printed out the result is the element on the corner mark.

If you are removing all the elements, it is as shown. With a corner mark, the operation of the array is the same. (list does not write length, why not write length and write size?) )

Only it has this method of removal, set does not have, set only iterator.

Now then take the elements in the collection, in a different form, and use the iterator to borrow it. Instead of doing the output action directly, set an object to receive the returned element.

At the time of saving, the Add method parameter list is promoted by the object class by means of polymorphic knowledge points.

DOS Results There are two questions , one is why there are next:abc1 appear, and the second is why there is an abnormal appearance?

Have not seen this package, to learn to consult the API documentation, go to util package in the exception, search concurrentmodificationexception.

What is concurrent modification? This is a common problem that arises in our development.

The problem is that there are three elements stored in the list collection, and the length of the collection is extended once. Three are saved, and the length is three. In the set of length three, we call its iterator, Iterator It=it.iterator (); The value of the iterator is three, in other words, at this time the iterator is called, and the iterator knows only three elements in the collection. The iterator starts out as a three-element method. In the later judgment, the collection adds the element abc9, extending the length, but the iterator does not know. Iterators iterate over this element incessantly because it is now an iterator manipulating these elements, and in the process of an iterator operation, the element is manipulated by the collection (List.add ("ABC9")). An iterator is an object, a collection is an object, and the two are not the same thing. Both the collection and the iterator are modifying the element at the same time. will cause problems with the iteration. This is why the exception was created.

How to resolve this exception. If it is necessary to make a judgment in the operation, to add elements to the list collection, how to modify? Since iterations and collections are accessed at the same time, concurrency is caused. Then, when using the collection operation, the incessantly generation. When using an iterative operation, do not assemble, do not conflict. Instead of a collection during an iterative operation, you can only iterate (meaning to add elements with iterations), but there are only two ways of doing it, either the collection or the iteration, or the iteration from the collection. Therefore, when iterating, the collection cannot participate, and the iteration is done. But there are only three methods in the iteration, one to judge, one to get, the last to delete, and no method to add.

The iterator itself has limitations, and the iterator below has sub-interfaces, Listiterator list iterators.

The sub-interface provides added methods, modifications, fetches, deletions, and judgments. How do I get a sub-interface object? There is another method in the list collection that is not described, that is, Listiterator (), which can be used to get a list iterator. This iterator, only the list has (the entire collection frame system, there are iterators exist).

There are some other methods in the Listiterator interface.

The Hasprevious method is to determine whether there are elements in front.

After the traversal, the result of Hasnext is false, and the result of Hasprevious is true. So, here the iterator can go forward.

The ABC3,ABC9,ABC1 in the DOS result is the Hasnext method (not 3,2,1, because the set method is used earlier, and the element of the 0-point mark is changed).

Java-preliminary Understanding-Common Object API (Collection framework-listiterator interface)

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.