List (JDK1.7) (1)

Source: Internet
Author: User
Tags addall concurrentmodificationexception

Java.util package.

Unlike set, the list allows repeating elements. That is e1.equals (E2).

Partial method definition
    • int size ();

Returns the number of elements in the list and returns Integer.max_value if the Integer.max_value is exceeded

    • Boolean isEmpty ();
    • Boolean contains (Object O);

For target element O, if there is at least one element e:o==null in the list? E==null; O.eqauls (E).

If the target element is not comparable, an exception will be thrown: classcastexception.

If the target element is null and the list does not allow null elements, an exception is thrown: NullPointerException.

    • Iterator<e> Iterator ();
    • Object[] ToArray ();

Returns all elements in the list in the appropriate order.

The returned array is safe because its references are not maintained by the list. In other words, the method must request a new array, even if the list itself is array-based. Therefore, the caller is free to modify the returned array.

As a bridge, the method builds an array-based and collection-based API.

    • <T> t[] ToArray (t[] a);

The run-time type of the returned array is the specified array type.

If the list happens to be of that type, it is returned directly. Otherwise, a new array must be requested, and the new array is exactly the size of the list.

If the list is of the specified type and there is space remaining in the array, the unused array position is all null. -This is useful because you can determine the length of the list. The premise is that the caller knows that the list does not contain any null elements.

    • Boolean Add (e e);

A list that supports this operation may restrict the elements that are allowed to join the list. For example, some lists might reject adding null as an element, while other lists might restrict the type of the element.

Unsupportedoperationexception, if the list does not support the Add method.

ClassCastException, which specifies the class of the element, does not allow it to be added to the list.

NullPointerException, if the specified element is null, and the list does not allow a null element.

IllegalArgumentException, if the element has some properties, it is not allowed to be added to the list.

    • Boolean remove (Object O);

Delete the first occurrence of the element, which is the lowest subscript: O==null? Get (i) ==null:o.eqauls (get (i);

If the list does not contain such an element, it will not change.

ClassCastException, if the type of the specified element cannot match the list.

NullPointerException, if the specified element is null, and the list does not allow a null element.

Unsupportedoperationexception,list does not support the Remove method.

    • Boolean containsall (collection<?> c);

Returns true if all elements in the specified collection are included in the list.

ClassCastException if the type of one or more elements in the specified collection does not match the list

NullPointerException, specifies that the collection contains one or more null, and the list does not allow null elements.

    • Boolean AddAll (collection<? extends e> c);

Unsupportedoperationexception

ClassCastException

NullPointerException

IllegalArgumentException

    • Boolean addall (int index, COLLECTION<? extends e> c);

Inserts all the elements in the specified collection at the specified subscript.

Indexoutofboundsexception if the subscript is out of range.

    • Boolean RemoveAll (collection<?> c);

Deletes all elements in the list that appear in C.

    • Boolean retainall (collection<?> c);

Preserves all elements that appear in the specified collection.

NullPointerException, if the list contains a null, and the specified collection is not allowed null.

ClassCastException

    • void Clear ();
    • Boolean equals (Object O);

The two list equals the same condition: they contain the same elements in the same order.

    • int hashcode ();

    • E get (int index);
    • e Set (int index, e element); Replace an element that already has a subscript
    • void Add (int index, E element);
    • E Remove (int index);
    • int indexOf (Object o);

If present, returns the first occurrence of the subscript. If not present, returns-1.

    • int lastIndexOf (Object o);
    • Listiterator<e> Listiterator ();
    • listiterator<e> listiterator (int index); Starts the traversal from the specified subscript
    • list<e> sublist (int fromIndex, int toindex);

Returns a view. Contains the left subscript and does not contain a right subscript.

The returned list is based on the original list, so the non-structural changes made in the returned list are reflected in the original list, and vice versa.

Any operation on a local scope list can represent the entire list by passing a sublist view. For example, the following statement removes some elements from the list:

List.sublist (from, to). Clear ();

ArrayList

An adjustable-size array-based implementation of the list interface. All optional list operations are implemented, and all elements, including NULL, are also allowed.

In addition, the class provides a way to manipulate the size of the underlying implementation array.

This class is basically the same as the vector class except that the ArrayList is not synchronous.

Methods to run in constant time: size, IsEmpty, get, set, iterator, Listiterator. The constant time of amortization, i.e. O (n): Add. Roughly speaking, the runtime of other methods is linear. The constant factor is smaller than the LinkedList.

Each ArrayList instance has a capacity of capacity. This is the size of the underlying array. Typically, the capacity is at least as large as the size of the list. Since adding a dollar has a constant amortization time, the details of the growth strategy are not clearly defined.

One way to scale up before adding a large number of elements is to use the ensurecapacity operation, which reduces the number of capacity adjustments.

Note that the ArrayList is non-synchronous. If multiple threads access the same ArrayList instance at the same time, and at least one thread modifies the list structure, there must be an external synchronization mechanism. This is usually done by synchronizing some objects that encapsulate the list, and if there are no such objects, the list should be wrapped using collections.synchronizedlist (). It is best to do so at the time of creation, avoiding unintended non-synchronous access to the list. (Structural modification refers to the addition or deletion of one or more elements, or the resizing of the underlying array, modifying only the value of the element, not the structural modification).

The iterator () method in ArrayList and the iterator returned by Listiterator () are fast failures. If a list is structurally modified at any time after the iterator has been created, the iterator throws a Concurrentmodificationexception exception, in addition to the method provided by iterator itself. In the face of simultaneous changes, iterator will quickly and simply fail, rather than be uncertain about future risky, non-deterministic behavior.

Note that the fast failure mechanism provided by iterator does not guarantee thread safety. The fast failure mechanism should only be used to detect bugs.

Abstractlist Source

The abstract class implements some of the methods.

ArrayList Source Details

ArrayList inherits from the Abstractlist abstract class.

Member variables

Construction method

Get () method

Set () method

Add () method

Expansion

Contains () method

Remove () method

Clear () method

Clone () method

Converting to arrays

Iterators iterator and Listiterator

"About the Remove () method in ITR"

Notice that the Lastret represents the last read position, and when this position is-1, the Remove method throws IllegalStateException. And this position is-1, one is initialized, has not been called next (), and the other is after the Remove () method is called. Therefore, in both cases, the remove () method cannot be called, that is, the remove () method cannot be called consecutively and must be combined with next () and remove () after next ().

In addition, when the element is deleted, the expectedmodcount in the iterator is updated synchronously, so that it is consistent with the Modcount in the list, so that the checkforcomodification () is passed and is not inherently thread-synchronized. When multiple iterator access the same list at the same time, it is still unsafe.

"Listitr"

On the basis of ITR, the method of forward traversal is added, which is equivalent to a bidirectional ergodic device. The set () and add () methods are added to allow the Walker not only to delete elements, but also to modify and add elements.

In particular, the Add () method and the Remove () method both modify the Lastret value to 1, so you need to read the Lastret method, such as set (), remove () cannot be called after both methods, you need to call the next () method first.

The Add () and remove () methods of the list are structural modifications, so the Add () and remove () methods in the iterator need to update the value of the modcount synchronously, otherwise it will be detected differently when the next deletion or change is checked. and throw concurrentmodificationexception.

Child List View

List (JDK1.7) (1)

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.