Java LinkedList previous next

前端 未结 3 1482
轮回少年
轮回少年 2021-01-02 08:46

is there anything similar to .Net\'s LinkedListNode<(Of <(T>)>)..::.Next and LinkedListNode<(Of <(T>)>)..::.Previous prop

相关标签:
3条回答
  • 2021-01-02 09:17

    You need to use a ListIterator you use that to traverse the List in either direction. The iterator contains methods such as previous() and next(). Check it out in the Javadocs.

    To get the ListIterator for your current list, call its listIterator(int index) method.

    0 讨论(0)
  • 2021-01-02 09:24

    Use the List interface's listIterator() method to get a ListIterator object. From there you can use the hasNext(), next(), hasPrevious(), and previous() methods to navigate the list. Here's a very simple example using ListIterator.

    import java.util.LinkedList;
    import java.util.List;
    import java.util.ListIterator;
    
    ...
    
    List<String> myList = new LinkedList<String>();
    
    myList.add("A");
    myList.add("B");
    myList.add("C");
    
    ...
    
    ListIterator<String> it = myList.listIterator();
    
    if (it.hasNext()) {
        String s1 = it.next();
        System.out.println(s1);
    }
    

    The example code should print "A".

    0 讨论(0)
  • 2021-01-02 09:38

    In general, you don't work directly with the nodes of a List in Java. You should probably look into ListIterator, an instance of which is accessible via the listIterator() method.

    0 讨论(0)
提交回复
热议问题