java - iterating a linked list

后端 未结 6 1061
半阙折子戏
半阙折子戏 2020-12-08 20:18

if I use a for-each loop on a linked list in java, is it guaranteed that I will iterate on the elements in the order in which they appear in the list?

相关标签:
6条回答
  • 2020-12-08 20:28
    iterate LinkedList by using iterator
    
    LinkedList<String> linkedList = new LinkedList<String>();
    linkedList.add(“Mumbai”);
    linkedList.add(“Delhi”);
    linkedList.add(“Noida”);
    linkedList.add(“Gao”);
    linkedList.add(“Patna”);
    
    Iterator<String>  itr = linkedList.iterator();
     while (itr.hasNext()) {
     System.out.println(“Element is =”+itr.next());
    
     }
    

    Reference : Java Linkedlist Examples

    0 讨论(0)
  • 2020-12-08 20:30

    Each java.util.List implementation is required to preserve the order so either you are using ArrayList, LinkedList, Vector, etc. each of them are ordered collections and each of them preserve the order of insertion (see http://download.oracle.com/javase/1.4.2/docs/api/java/util/List.html)

    0 讨论(0)
  • 2020-12-08 20:35

    I found 5 main ways to iterate over a Linked List in Java (including the Java 8 way):

    1. For Loop
    2. Enhanced For Loop
    3. While Loop
    4. Iterator
    5. Collections’s stream() util (Java8)

    For loop

    LinkedList<String> linkedList = new LinkedList<>();
    System.out.println("==> For Loop Example.");
    for (int i = 0; i < linkedList.size(); i++) {
        System.out.println(linkedList.get(i));
    }
    

    Enhanced for loop

    for (String temp : linkedList) {
        System.out.println(temp);
    }
    

    While loop

    int i = 0;
    while (i < linkedList.size()) {
        System.out.println(linkedList.get(i));
        i++;
    }
    

    Iterator

    Iterator<String> iterator = linkedList.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next()); 
    }
    

    collection stream() util (Java 8)

    linkedList.forEach((temp) -> {
        System.out.println(temp);
    });
    

    One thing should be pointed out is that the running time of For Loop or While Loop is O(n square) because get(i) operation takes O(n) time(see this for details). The other 3 ways take linear time and performs better.

    0 讨论(0)
  • 2020-12-08 20:36

    As the definition of Linkedlist says, it is a sequence and you are guaranteed to get the elements in order.

    eg:

    import java.util.LinkedList;
    
    public class ForEachDemonstrater {
      public static void main(String args[]) {
        LinkedList<Character> pl = new LinkedList<Character>();
        pl.add('j');
        pl.add('a');
        pl.add('v');
        pl.add('a');
        for (char s : pl)
          System.out.print(s+"->");
      }
    }
    
    0 讨论(0)
  • 2020-12-08 20:38

    Linked list is guaranteed to act in sequential order.

    From the documentation

    An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

    iterator() Returns an iterator over the elements in this list in proper sequence.

    0 讨论(0)
  • 2020-12-08 20:50

    Linked list does guarantee sequential order.

    Don't use linkedList.get(i), especially inside a sequential loop since it defeats the purpose of having a linked list and will be inefficient code.

    Use ListIterator

        ListIterator<Object> iterator = myLinkedList.listIterator();
        while( iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    
    0 讨论(0)
提交回复
热议问题