im learning about arrays and lists in java and i come across linked lists and i have yet to think of a useful way of using them... mabe im not fully understanding their capabilites? if anyone could clear this up greatly appreciated!

If an array exceeds the size you need to copy all the elements into a new larger array which can be expensive. You can also insert or remove items in the middle of the list easily whereas in an array you have to move elements up or down to do the same thing (assuming you want the ordering to remain the same).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I thought an ArrayList in Java was a resizeable array and not a linked list... could be wrong though, it could actually be a linked list.

Basically:

array type data structure = fast random access to any element in the data, resizing can be expensive if the size exceeds current capacity, insertions/removals not at the end mean elements have to be moved.

Is a List different to an ArrayList? I'm not a Java programmer... If a "normal" List is a linked list then there's no point rolling your own.

Even if the interface is the same doesn't mean that the implementation is... you typically use data structures based on the most common operations you will be doing with the data (so random access => array type structure, lots of insertions/deletions, linear access only => linked list).

You may want to read up on "Big O" notation (and not in Cosmopolitan, it means something different there), which gives an idea of how fast you can expect an algorithm to perform with different data structures.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

In addition to implementing the List interface, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list. These operations allow linked lists to be used as a stack, queue, or double-ended queue.

im learning about arrays and lists in java and i come across linked lists and i have yet to think of a useful way of using them... mabe im not fully understanding their capabilites? if anyone could clear this up greatly appreciated!

Linked lists are good when you are doing a lot of random insertions, in a linked list this operation is very cheap whereas in an array you need to shuffle things around. For instance, say you have an array with one billion items in it, and you want to insert some item right in the middle. You will need to take the 500 million items after the insertion point, and shift them up 1 place up the array to make space for the element you're about to insert. In a linked list? No problem, find the element before and the element after where you want to insert your element, and simply modify their "next element" (and "previous element", if applicable) pointers, which naturally inserts the new element into the list without touching any element other than the two surrounding the insertion point.

On the other hand, you cannot have random access in a traditional linked list - if you want to read the Nth element, you need to walk through the linked list until you reach it, costing you N operations, instead of 1 operation for an array, since you can just directly get the element you want in an array. As you can see, they both have pros and cons.

The way you use the list is exactly the same, because they ultimately perform the same function (they are both lists) but depending on what you do with the list - what kind of operations, what sort of data you're manipulating - you should select the most appropriate one for your particular situation to optimize performance.

It is worth noting that unless you are in a very particular situation, linked lists do not win over arrays, simply because arrays are faster in modern hardware due to cache coherency and sequential memory access, whereas linked list traversal tends to jump all over memory. So the places where you would realistically use a linked list, other than for learning, are quite limited.

Edited by Bacterius, 30 March 2013 - 09:49 PM.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Linked Lists and Doubly linked lists are the pathways to understanding tree and graph data structures, which are very very useful. So think of Linked Lists (single or double) as a gateway drug

External Articulation of Concepts Materializes Innate Knowledge of One's Craft and Science
Beginner in Game Development? Read here.Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts

Spoiler

How on earth can you go around telling lies, be shown that they are lies, then continue to ignore the evidence to the contrary. I'm not qualified to deal with this, sorry. - HodgmanNever crash to desktop. That's the equivalent of a Starbucks rep punching you in the face when you try to order a coffee with soymilk when they run out of soymilk. - KoobazaurFor programmers, R & D means "Research & Duplicate".Just remember... XML is like violence. If it doesn't work, you just aren't using enough of it... - MoeEvery time I go into my account settings I feel like I've never used the internet before. - booleanPerson X: Do you want to live forever?
Person Y: No, but I want to live a long life.
Person X: Only cowards live that long.
Person Y: And yet, we travel to seek the counsel of these "cowards".