Hi,
How sure can I be about the order of the elements in a LinkedList in a for each loop? Does it goes from last to first?
Thanks
Printable View
Hi,
How sure can I be about the order of the elements in a LinkedList in a for each loop? Does it goes from last to first?
Thanks
For the quick answers about how a class works, write a small simple program, execute it and see.
If you have problems or questions, post the code here with the results and your questions.
Traversing a LinkedList in a particular direction will always give you the same order of elements unless you explicitly modify the order of elements.
Yes, if you traverse it in that order... java.util.LinkedList is a doubly-linked list, so can be traversed in either direction. It is also a Queue and a Deque - see the API docs.Quote:
Does it goes from last to first?
It is not knowledge, but the act of learning, not possession, but the act of getting there which generates the greatest satisfaction...
F. Gauss
Hi,
Thanks Norm and dlorde, I already have a program installed and working in testing, in a testing environment, but this question is very important to the correct behavior of the application.
I guess dlorde already answer my question but just to be sure.
Example:
Pseudocode:
In this case, its positive I will get the items in order? Its the for loop normal behavior to get the elements in order always? Why I ask this? Because I couldn't find some site where its explained clearly, even when for now it doesn't represent a problem.Code:list.add("A");
list.add("B");
list.remove("A");
list.add("C");
for (Item item : list) {
System.out.println(item);
}
Thanks again,
Really? The API docs for List say "An ordered collection (also known as a sequence)". Wikipedia sums it up in the first line: "a linked list is... a data structure that consists of a sequence of nodes each of which contains a reference (i.e., a link) to the next node in the sequence".
If you can find a simple way to traverse that out of sequence, please let us know.
The purpose of computing is insight, not numbers...
R. Hamming
Hi,
I didn't said other implementations of Linked List, and even I didn't said Java. I got confused, I read somewhere that some Collections doesn't guarantee the order, that's why I ask, to be sure and someone with more knowledge told me the right direction, like you already did.
Well, thanks again.
Oh, OK - sorry, I thought that's what we were talking about - all the thread posts were about LinkedList and this is a Java forum... :confused:
Collection classes like HashMap don't guarantee order, but they're not sequential, ordered collections. A LinkedList clearly is - ordering is implicit in the links between nodes, and the API docs show it is a Queue and can be used as a stack. The API docs should be the first point of reference when trying to establish how a class behaves - although sometimes it takes a while to pin it down ;)Quote:
I got confused, I read somewhere that some Collections doesn't guarantee the order.
Computers are to computing as instruments are to music. Software is the score whose interpretations amplifies our reach and lifts our spirits. Leonardo da Vinci called music the shaping of the invisible, and his phrase is even more apt as a description of software...
A. Kay
Hi,
You see, that is where I got confused. You right, I read somewhere some collection in some language doesn't provides that, I searched the API and even when thats was implicit I couldn't interpret that.
Your right, I remember now, Collections iterator, it doesn't guarantee the order, unless an implementing class provides a guarantee. And Linked Lists, by nature, provides that.
I really appreciate all your help, like you said, sometimes it takes a while to pin it down, I will be aware of that next time.
Thanks,
You only know something if you can write working code.
This idea that you can test code into working is the root of much evil. Code should be designed to work, not tested until it seems to work.
The OP is absolutely on the right track here not being satisfied with code that's mere working. I suggest you follow his example instead of ignorantly advocating a sloppy and dangerous practice.
Not my idea at all.
Read the API doc, get some ideas of what you need, write a simple test program to get the technique down, copy the working code/techniques into your program.