Java Collection (i): List, Iterator, Array, ArrayList, linklist

Source: Internet
Author: User
Tags iterable

After graduating so long, the collection in the Java standard library has not been carefully understood, ready to learn to learn.

Set (Collection): The whole of one or more determined elements is called a set -The interpretation of the Baidu Encyclopedia. In other words, it is the container that is loaded with elements, which can be of any type.

In our daily development, this kind of tool is essential, so, well understand their characteristics, can let us write more appropriate code, but also in the practice of internal strength.

------------------------------------------------------------------------------------------

In Java collections, I think you can classify two classes: an ordered set (List : The elements are arranged in a certain order) and an unordered collection (set : The element is unordered, and this attribute also determines that the element is not repeatable).

For a collection, there is an indispensable accessibility tool, which is the iterator (Iterator), why do you say so? Let's look at the code in Java

Collection Interface :

From the above code, in the Collection interface, there is a iterator<e> Iterator () method, which returns: An object that implements the Iterator interface. Use this iterator object to access the objects in the collection in turn.

Iterator Interface:

Presumably most people use iterators to iterate through the collection, with the Hasnext () and remove () methods above, which can be done as follows:

New arraylist<string>= list.iterator ();  while (Iter.hasnext ()) {// How to also have elements    String element = Iter.next (); // get element     // Do something with element}

However, some people see the above code, it may feel a bit cumbersome, then Java provides a more brief way: For Each loop.

 for (String element:list) {  //dosomething with element;  }

The price of brevity is that the collection must be implemented with the Iterable interface, which is defined as follows:

The ForEach method and the Spliterator method are all default methods (the JAVA8 begins to allow this kind of action, providing a default implementation, not enforcing it.) Reference: http://blog.csdn.net/lin6286878/article/details/53464804), so when implementing the Iterable interface, only the iterator method can be implemented.

So in Java, the Collection interface expands the Iterable interface, so that it represents a collection object that can use for each to reach the purpose of traversal

----------------------------------------------------------------------------------------------------

Once you know the relationship between a collection and an iterator, the ArrayList object has the properties of the array, so it's similar to the array; Needless to say, we're talking about linklist objects.

The Add method in the Linklist object, each time you add an element at the end of the list, if I want to insert an element at a specific location in the Listlist object? Rest assured, Java provides us with the Listiterator interface, adding the Add method on top of the Iterator interface, ensuring that the linked list can be inserted at any point in the element. The pointer in the iterator is between two elements, and the position of the inserted element is the number of elements of the collection element + 1.

Since iterators are so convenient, can multiple iterators Act on the same set at the same time, the answer is: yes, but there is only one iterator that can change the collection (others can only read).

If multiple iterators can be changed, then the data in the collection becomes messy, so how do you ensure that there is only one iterator that modifies the collection?

1) Each iterator maintains a counter for this write operation,

2) The set itself maintains a counter for the rewrite operation,

3) at the beginning of each iterator, check that the values of the two counters are consistent and pass if they are consistent, otherwise throw modificationexception exceptions.

Java Collection (i): List, Iterator, Array, ArrayList, linklist

Related Article

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.