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.
1 Answer
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.
Iterator. Java'sLinkedListwill never hand you an instance of its internalNodeimplementation.Mapimplementations will let you use theirEntryobjects. Typically Java wants to hide the actual implementation so that they can change it without affecting code.LinkedList", do you mean a structure where a later node in the list can point back to an earlier node (creating a cycle)?LinkedListdeliberately 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 normalList.