JAVA backend interview must know the similarities and differences --ListIterator and Iterator

When using Java collections, you require the use of Iterator. But there is a set of java iterator ListIterator, it can be used when using the List, ArrayList, LinkedList and Vector. Both iterators What difference does it make? We analyzed in detail below. There is little need clear when the iterator to the position is a position before the elements.

First look at the Iterator iterator methods and ListIterator what.

Iterator iterator methods have included :

  • hasNext () : If the iterator is pointing back position as well as the elements, it returns true, false otherwise
  • Next () : returns the set point position behind the element Iterator
  • Remove () : Delete the collection element rearwardly directed position Iterator

ListIterator iterator methods are included :

  • the Add (E E) : a list of the specified element is inserted, the insertion position before the current position of the iterator
  • hasNext () : When forward to traversing the list, if the list iterator and there are elements Returns true, false otherwise
  • hasPrevious () : If the reverse traversing the list, a list iterator still ahead element, returns true, false otherwise
  • Next () : returns the element rearwardly directed position in the list ListIterator
  • nextIndex () : returns the index of the list element in position behind the desired ListIterator
  • Previous () : Return to the previous list element points to a location ListIterator
  • previousIndex () : returns the index position in front of the list of required elements ListIterator
  • Remove () : Delete next () or previous () Returns the last element from the list (a mouthful, meaning that the use hasNext iterator () method, remove ListIterator pointing element rear position; when used iterator hasPrevious () method, remove ListIterator point position in front of the element)
  • SET (E e) : e changed to the specified element from the list next () or previous () the last element of the last element to return returned

Same point

  • Are iterators, when it is necessary to set the interference elements which do not need to traverse the traversing process, both of the iterator can be used.

difference

  • Different scope, Iterator can be applied to all collections, Set, List and Map collections of these subtypes. The ListIterator only for List and its subtypes.

  • There ListIterator add method, you can add objects to the List, but Iterator can not.

  • 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 not.

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

  • Delete operation can be realized, but can be achieved ListIterator modify objects, set () method can be achieved. Iterator traversal only, it can not be modified.
ArrayList<String> stringArrayList1 = new ArrayList<String>();
        ArrayList<String> stringArrayList2 = new ArrayList<String>();

        stringArrayList1.add("ok");
        stringArrayList1.add("hello");
        stringArrayList1.add("world");

        stringArrayList2.add("好的");
        stringArrayList2.add("你好");
        stringArrayList2.add("世界");

        stringArrayList1.addAll(stringArrayList2);
        ListIterator<String> iterator = stringArrayList1.listIterator();

        System.out.println("从前往后输出:");
        while (iterator.hasNext()){
            System.out.println("next="+iterator.next());
        }
        System.out.println("\r\n从后往前输出:");
        while (iterator.hasPrevious()){
            System.out.println("previous="+iterator.previous());
        }

Note: make sure to output from the front to back, before it can be output from the rear forward.

Written in the last

  • First: watching the thumbs up, thank you for your approval;
  • ...
  • Second: hand forward, to share knowledge, to allow more people to learn;
  • ...
  • Third: remember point concerns, updated daily! ! !
  • ...

JAVA backend interview must know the similarities and differences --ListIterator and Iterator

Guess you like

Origin blog.51cto.com/14409778/2416416