1

I study java.util.LinkedList and listIterator for several days, and feel it is really hard to implement some functions such as loop LinkedList, and it does not have Node structure(I mean something like element, next node, previous node, I do know there is something similar in iterator), how you guys did this or you just create yourselves LinkedList class? Anyone can help me figure this out? I mean just use java.util.LinkedList collection.

7
  • 1
    Use an Iterator. Java's LinkedList will never hand you an instance of its internal Node implementation. Commented Jan 6, 2014 at 21:09
  • Thanks Buddy! I just want to make sure i did not missed anything in internal class about LinkedList. Commented Jan 6, 2014 at 21:14
  • As far as I know, only Map implementations will let you use their Entry objects. Typically Java wants to hide the actual implementation so that they can change it without affecting code. Commented Jan 6, 2014 at 21:15
  • By "loop LinkedList", do you mean a structure where a later node in the list can point back to an earlier node (creating a cycle)? Commented Jan 6, 2014 at 21:15
  • 1
    LinkedList deliberately disallows you from manipulating its internal structure, and disallows you from creating cyclic linked lists, etc. It's designed to only let you use it as a normal List. Commented Jan 6, 2014 at 21:16

1 Answer 1

4

A List in Java is simply an ordered sequence of objects. This concept doesn't allow for things like cycles, trees, etc. There are several implementing classes like LinkedList, ArrayList, etc., that implement Lists in different ways; the most important distinction between them is that some operations are faster with certain kinds of implementations (e.g. inserting is faster in a LinkedList than an ArrayList). However, they all implement the same basic concept, and you can't use them for something different.

If you want a different kind of data structure, you'll either need to implement it yourself (which should not be too difficult) or look for some other, more general package to handle "graph" structures. Unfortunately I don't know of one offhand.

Sign up to request clarification or add additional context in comments.

1 Comment

It really helps me a lot. Thank you for your answer and patience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.