开发者

Java Util Linked List - how to find next?

When using Java LinkedList how do you find out the element's next or previous relationships?

I mean, in a regular linked list I would do something like this:

Node node1 = new Node();
Node node2 = new Node();
LinkedList list = new LinkedList();
list.add(node1);
list.add(node2);

//then my node1 will know who it's next is:
assertEquals(node2, node1.next());

where Node is my own container for data/object.

But in Java's LinkedList, the data does not seem to be modified. So how do I actually find out who the "next" (or "previous" in the case of doubly-linked l开发者_JAVA技巧ists) element is?


You can't. LinkedList is just an implementation of List and offers about nothing more. You'd need to make your own.

For node1.next() you'd need a reference from node1 to the List. Actually, you'd need multiple references, since node1 may be there multiple times. Moreover, it may be contained in multiple Lists.

Maybe you can use ListIterator for this.


I don't know what Node class you're using, but LinkedList<T> has its own internal node class, which you don't get access to. Calling add will add a value to the list - you can't explicitly insert a node holding a value, or access the nodes themselves in any other way. Yes, that can be a pain sometimes.

If you need a linked list with a public encapsulation of the node as well, you'll need to find a different implementation or roll your own.


Best Solution: Make your own next and last Links when constructing an new List Item Object:

Just have a lastInserted Object somewhere more globally

public MyLinkedListItem(){
    if(lastInserted != null){
        lastInserted.next = this;
        this.last = lastInserted;
    }
    lastInserted = this;
}


You can use an iterator to move through the nodes such as:

    Node node1 = new Node();
    Node node2 = new Node();
    LinkedList list = new LinkedList();
    list.add(node1);
    list.add(node2);

    Iterator <Node> m_iterator=list.iterator();

    //set iterator to first node
    m_iterator.next();

    //then my node1 will know who it's next is:
    assertEquals(node2, m_iterator.next());


The "linked" part of the LinkedList class name only refers to its implementation. The interface does not expose explicit methods to do what you want.

LinkedList implements the Collection (and List) interface, so given an index i of an element in the list list, you can get previous and next elements with list.get(i-1) and list.get(i+1), respectively. For a LinkedList, the implementation of these methods are quite slow. If you do a lot of prev/next operation, consider to implement your list or to use an ArrayList instead.


I have to disagree with the accepted answer. You can as long as you have the head element. Once you lose reference to the first element by deleting and not returning the next element or insert an element before the first element and not return it, you will not be able to do your searches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜