Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61061

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 lists) element is?

Upvotes: 23

Views: 76866

Answers (6)

jasonKoolRay
jasonKoolRay

Reputation: 21

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());

Upvotes: 2

Seivi
Seivi

Reputation: 21

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;
}

Upvotes: 2

Mukus
Mukus

Reputation: 5033

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.

Upvotes: 0

frm
frm

Reputation: 3486

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500675

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.

Upvotes: 11

maaartinus
maaartinus

Reputation: 46422

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.

Upvotes: 21

Related Questions