List Collections Framework

Collections Framework Overview

1, a frame set (Introduction, Collection method, iterator)

  • Brief introduction

    The origin of the set of frame : for storing a plurality of data objects, the object with a set of multiple stores.
    The stored data (data structure) are different, the storage container will have more, thereby forming a framework of the collection system.

  • Case column analysis
    from the following figure , the circle is installed if the nature of the fluid container, filled candy square of the container, the container bottom right circular drawn into three round containers List, three bottom left square square container to the extraction vessel set, the figure shows their storage rule is the same, from bottom to top are stored, and their basic functions are stored things, List, and then further on to set their extraction feature is collinear storage, which is spoken today originated collections .
    Here Insert Picture Description

  • Collection Methods

    In fact, most of the methods are described for some reality.
    Here Insert Picture Description
    To provide a better understanding of the following Web sites : http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

  • Iterator
    explain

    Iterator (Iterator) is an object that can be used to traverse the vessel Standard Template Library some or all of the elements, each iteration determines the address of the representative object container. Iterator modify the conventional pointer interface, called iterator is a conceptual abstract: those acts something like an iterator can be called iterators. However, there are many different iterator capabilities, it can be abstract and generic algorithms container of organic unity.
    Iterator provide some basic operators: *, ++, ==, =,! =. These operations and C / C ++ interface pointer consistent when "operating element array." Except that, the iterator is called complex pointers with the ability to traverse complex data structures. Depending on which operating mechanism underlying its data structure traversal. Therefore, each container type must provide their own iterators. Indeed containers each iterator which is defined in a nested manner in the interior. Therefore, the same interfaces to different iterator model is different. This directly leads to the conceptualization of generic programming: conduct all operations use the same interface, although they are typed different.

    Features

    Iteration allows developers to support foreach iteration in a class or structure, without having to implement the entire IEnumerable or IEnumerator interface. Just to provide an iterator to traverse the data structure class. When the compiler detects iterator will automatically generate Current IEnumerator IEnumerable interface or interfaces, the MoveNext and Dispose method.

    Feature

    1. An iterator is a piece of code can return an ordered sequence of values of the same type;
    2. iterator method as a code body, the operator, or get accessor;
    3. iterator code uses yieldreturn statement returns each element in turn , yield break will terminate the iteration;
    4. multiple iterations may be implemented in classes, each iteration is the same must have a unique name as any member class, and can be a client in a foreach statement, the code calls as follows : the foreach (int X in SimpleClass.Iterator2) {};
    5. the iterator must return type is any one of IEnumerable and IEnumerator;
    6. iterator is a statement block generates an ordered sequence of values, there are different from or the presence of a plurality of conventional block of statements yield statements;
    7. iterator is not a member, it is only the implementation of the function member, understand that it is important, by a member of the iterators implement, or may be other possible and the covering member is not possible by overloads of implementation;
    8. iterator block in C # syntax elements is not unique, they are limited in several ways, and the main With semantic function member declaration, they are just a block of statements only on grammar;
    9.yield keyword is used to specify the value returned. Yieldreturn statement upon arrival, will save the current position. From this position the next time it resumes execution when calling the iterator. Iterator particularly useful for collection class, which provides an easy way to iterate infrequently used data structures (e.g., binary tree).

    2, the set of frame List (ArrayList specific methods, specific iterator, the object-specific characteristics, demonstrated a growth factor)

    • 1, list set with respect to the specific method of collection set
     public static void sop(Object obj)
    {
        System.out.println(obj);
    }
    public static void method()
    {
        
        ArrayList al = new ArrayList();
    
        //添加元素
        al.add("java01");
        al.add("java02");
        al.add("java03");
        
        sop("原集合是:"+al);
        //在指定位置添加元素。
        al.add(1,"java09");
    
        //删除指定位置的元素。
        //al.remove(2);
    
        //修改元素。
        //al.set(2,"java007");
    
        //通过角标获取元素。
        sop("get(1):"+al.get(1));
    
        sop(al);
    
        //获取所有元素。
        for(int x=0; x<al.size(); x++)
        {
            System.out.println("al("+x+")="+al.get(x));
        }
    
        Iterator it = al.iterator();
    
        while(it.hasNext())
        {
            sop("next:"+it.next());
        }
    
        //通过indexOf获取对象的位置。
        sop("index="+al.indexOf("java02"));
    
        List sub = al.subList(1,3);
    
        sop("sub="+sub);
    }
    • 2, the difference Iterator.remove () and Collection.remove () is
      that they do not practice, you would either operate simultaneously, while taking advantage of the collection method, or to use an iterator unified operation

      Collection.remove()方法
      Here Insert Picture Description
      Iterator.remove()方法
      Here Insert Picture Description

    • 3, with the difference Iterator of ListIterator

    1. There ListIterator add () method, the object may be added to the List, but not Iterator

    2. ListIterator and Iterator has hasNext () and next () method may be implemented to traverse back order, but there ListIterator hasPrevious () and previous () method may be implemented reverse (forward order) traversal. Iterator can not.

    3. ListIterator index can locate the current position, nextIndex () and previousIndex () can be realized. Iterator not have this feature.

    4. Delete the object can be realized, but can be achieved ListIterator modify objects, set () method can be achieved. Iierator only traverse can not be modified.

The difference between the remove method of the iterator and remove methods of collection?

Only need to know the iterator in memory runs pointer. Iterators and collection of two objects, if the remove method is called in the iterator collection, will be reported currentMddifyException [current] abnormal change

The difference between the array and arrayList?

  1. fixed length array, arrayList conversely
  2. Once the array declaration can only put objects of that class

The difference arrayList and LinkedList?

  1. ArrayList data structure is an array of storage, resulting in a faster query speed, slow additions, the threads are not synchronized.
  2. LinkedList is a linked list data structure stored, additions and deletions fast, slow query [it is stored before a joint and last point of each object record]
  3. vector: [thread synchronization method Riga it's all locked its mark

Use LinkedList complete stack and defining the storage space of the queue?

In the same thing

Guess you like

Origin blog.csdn.net/zyp_baoku/article/details/90349723