"Java study notes," Chapter 13 - Object Container (unfinished)

In the course of the program is running, a lot of time needs to be stored temporarily unified management objects in a container, then if necessary and then remove the object.

1, Collection class

Collection structure may hold separate objects. In J2SE, comprising Collection List and the Set, List java.util.List relevant class implements the interface, objects are placed to follow the order to be arranged in the container object. Set java.util.Set related class implements an interface, do not accept duplicate object can have its own set of arrangement rules.

1) Introduction List Interface

java.util.ArrayList class implements the java.util.List interface, we must first learn what the List interface. List interface is a sub-interface java.util.Collection interface. The Collection interface is a sub-interface java.lang.Iterator. The Iterable interface to implement a iterator () method.

Required to achieve the Iterable interface returns to its class object that implements the interface java.util.Iterator. Indeed not find any class that implements the Iterator in the J2SE API, because Iterator to iterate the elements will be based on the actual container data structure, the data structure of the container implementation is hidden from the outside world, users do not know the structure, only need to know the implementation of Iterator method, you can remove the element. Iterator defined interfaces are as follows:

    package java.util;

    public interface Iterator<E>{

        boolean hasNext();

        E next();

        void remove();

    }

Collection interface extends Iterator interface that defines adding elements, removing elements, element length and other methods.

Collection and removing elements made in the element more generic definition, the Interface List The method of increasing index of the acquisition target, which shows the characteristics of List data structure, each element is added to the List of added sequentially, and the specified index to access elements.

List data structure characteristic is added to each element of the List are added sequentially, and may specify the index to access the element. List may use arrays (Array) or a serial link (LinkedList) to implement this feature, the former is implemented in J2SE java.util.ArrayList, which is java.util.LinkedList. For the sequential addition of the access, use ArrayList better efficiency, the need for frequent changes in the order of the elements, use LinkedList would be better.

2)ArrayList

3)LinkedList

Guess you like

Origin www.cnblogs.com/Tom-1103/p/12150170.html