Forum Topic: Java: Linkedlists Vs. Arraylists

(455 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

ilikerps

Reply To Post Reply & Quote

Posted at: 8/2/06 08:14 PM

ilikerps NEUTRAL LEVEL 07

Sign-Up: 10/23/03

Posts: 38

Hello. I guess you could call this an efficiency tutorial, on any programming language that has ArrayLists or LinkedLists. However, this is focusing on Java's ArrayLists and LinkedLists. I haven't seen any scripting languages with a built-in LinkedList, but I don't think many need the possible extra efficiency either.

Code will be in Java, but it should be pretty easy to understand if you know any high level programming.

Okay, summary's at the bottom, but before then I'll describe what both are, and how they store data. If you don't know what either are, you may want to read the next paragraphs, though. Note that for my examples I use Strings, but any kind of data could be used.

Both LinkedLists and ArrayLists store groups of data. You can basically think of them as many variables in one nice, neat pacakge. Instead of writing things like

String username1 = "someUser";
String username2 = "someOtherUser";
String username3 = "someOtherOtherUser";
. . .

You can write and access it more concisely, using ArrayLists

ArrayList usernames = new ArrayList();
usernames.add("someUser");
usernames.add("someOtherUser");
usernames.add("someOtherOtherUser");
. . .

(For those of you who know what I mean, I'm not using generic programming to keep things simple)
Alright, so the writing thing isn't more concise, but think what happens when you want to dynamically access a username? What if the user gives you their id to find their name? Then you'd have to make a really long if() statement, whereas in a List, you can do
usernames.get(id);
One thing to note about ArrayLists is that they start at 0, however, rather than 1. So usernames.get(1) actually gets the second element.

Okay, in Java, you'd also need to type-cast the Object back to a String, but that isn't hard. You'd just do
(String) usernames.get(id);
And, if you know generic programming, you don't even need to add that.

Okay, anyways, these ArrayLists (and LinkedLists) are quite handy for varying amounts of data. These Lists allow you to store a theoretically unlimited amount of data, just by adding data to it; they don't care how much you add.

By the way, making and adding to a LinkedList is the same as for an ArrayList, though you would define it by replacing the word "ArrayList" with "LinkedList" in the declaration, like this:

LinkedList usernames = new LinkedList();

Accessing the data is a bit different however. I am going to assume your compiler is at Java 5.0 Compliance, now. Here is how you would print the contents of a LinkedList.

for(Object obj : usernames)
{
String username = (String) obj;
System.out.println(username);
}

This is called a for each loop. For every value in the LinkedList usernames, you get a variable of the Object class called obj. I casted this Object into a String, and then printed it out. Technically, you can do this to an array as well, but that would kind of defeat the purpose of it.

ARRAY LISTS
Okay, now for how specifically an ArrayList works. This knowledge is required for making good decisions on which one to use for which case. Both ArrayLists and LinkedLists have their drawbacks and advantages.

ArrayLists store their data in an array of Objects, and they are equip to resize this array at their whim. This resizing is done by creating a new array with a slightly bigger size than the last one, and copying all values of the old one into the new one.

As such, ArrayLists are really fast for random access. That is, asking for the third, eighth, etc. element of the ArrayList. They just look that up in memory and return it to you. It's really fast, and takes only 1 access to the ArrayList to get data (This is written in what is called Big-Oh notation. In this case , it would be O(1)). This means that whenever you want to access 1 element, it takes the same amount of time. An example of the use of this, is that if there is a Bank program, and a user wishes to access his BankAccount by an ID he has. This ID corresponds to the location of the BankAccount object in some ArrayList. It is trivial to get this information to the user. As you will see, it is not the same way for a LinkedList.

Great, you say! ArrayLists are great! But wait! What if I want to add or remove one of those BankAccounts, in the beginning/middle of the ArrayList? Good question! The answer is dreadful: In the case of adding a new element to the beginning of the ArrayList, every element of the array must be pushed down 1 row. That means accessing every single element! The same is true for removing the beginning element, except that they need to be pushed up 1 row. If you want to just add the data to the end of the ArrayList, however, you are in luck. You need not push up or down anything! However, adding/removing elements of an ArrayList requires an average of n/2 accesses (O(n/2)), where n is the number of elements in the ArrayList. If you are only adding/removing data to the end of the ArrayList, however, it is just O(1). That's good, but removing only from the end? That seems quite unlikely. So, ArrayLists are no good at removing or adding data anywhere but to the end.

Okay, this may seem intuitively obvious, but iterating (going) through the ArrayList takes O(n) accesses, 1 for each element. Unlike the adding/removing thing, this is the same for LinkedLists. Ah well, it can't be avoided. If you want to print out every value of an ArrayList, you have to, well, access each value of the ArrayList.

Summary: ArrayLists are great for random access, bad for adding/removing data, and mediocre at iterating through.

(continued in next post)


None

ilikerps

Reply To Post Reply & Quote

Posted at: 8/2/06 08:17 PM

ilikerps NEUTRAL LEVEL 07

Sign-Up: 10/23/03

Posts: 38

(had to split into 2 posts because it was too long)

LINKED LISTS
Ah. The LinkedList. It is more complex than the ArrayList, and basically it's opposite in terms of the three things looked at for ArrayLists. Let me try to explain simply.

A LinkedList stores is a pile of things called "nodes". In the most simple case, these nodes store 2 things:
1. The data they are supposed to contain
2. The next node

Let me revive my usernames example to explain. Consider this code:

LinkedList usernames = new LinkedList();
usernames.add("someUser");
usernames.add("someOtherUser");
usernames.add("someOtherOtherUser");

Now, at the end of these 3 declarations, we have 3 nodes in our LinkedList. If the LinkedList was a concrete thing, it may look something like this:
node0
Data: "someUser"
Next node: node1

node1
Data: "someOtherUser"
Next node: node2

node2
Data: "someOtherOtherUser"
Next node: null

As you can see, the first node points to the second, and the second points to the third. The last points to null, which basically means there is no data after it. What advantages/disadvantages are there to this? Let's have a look-see.

Random access is pretty much a joke for LinkedLists. The LinkedList has no idea about the order of it's nodes. All it knows is it's node's data and where to find the next node. So, to find, say, the 46th element of a LinkedList, the LinkedList must be iterated through 46 times to find the wanted value. If the wanted value is the 0th element, then it takes only 1 access. If it is the nth element, it takes n. This happens to average a n/2 accesses (O(n/2)), where n is the length of the LinkedList. If you had 10,000 users, and you wanted to access the 9,999th one, you'd have to actually wait for a little! No, that should not be tolerated when the ArrayList would get the 9,999th element in a few microseconds, if that much.

Darn. So, it sounds like LinkedLists suck. Why use them? Wait, what happens if I wanted to add or remove something to the beginning/middle of the LinkedList? Good question! Again. This is where LinkedLists really shine. Let me show you what happens when you need to add data (removing is similar, and takes the same amount of time). Assuming you have iterated to the right node, all that is required is changing the node you are currently on to have it's next node be the one that you are entering. Also, you should make the node that you are entering point to the node that no longer has anyone pointing to it. Simple! It takes only 1 access of something that is actually in the LinkedList. O(1) ftw!

Iterating through a LinkedList is no faster than through an ArrayList. If you want to print all values of the LinkedList you have to go through each one and print them. O(n) time.

Summary: ArrayLists are bad for random access, great for adding/removing data, and mediocre at iterating through.

FINAL SUMMARY
ArrayLists are good when you want random access, but are not good if you are going to be doing a lot of adding and removing data.
LinkedLists are just the opposite; they are bad when you want random access, but are good if you will be doing a lot of adding and removing data.

What if you want both random access and adding/removing data to be fast? Well, you'll have to decide which one you will be using more. If you are using both equally, then just pick whichever List you like better, since they will be both equally fast.


None

Negown

Reply To Post Reply & Quote

Posted at: 8/2/06 09:51 PM

Negown EVIL LEVEL 04

Sign-Up: 04/08/06

Posts: 100

Nice, but next time. Remember to link to Java: Main....thanks for the tutorial. I hope you can supply us with more :)


None

ilikerps

Reply To Post Reply & Quote

Posted at: 8/2/06 10:32 PM

ilikerps NEUTRAL LEVEL 07

Sign-Up: 10/23/03

Posts: 38

Oh, link to Java: Main? I thought it was link from Java: Main. Sorry.

Java: Main


None

Togukawa

Reply To Post Reply & Quote

Posted at: 8/26/06 08:55 PM

Togukawa LIGHT LEVEL 15

Sign-Up: 06/14/03

Posts: 1,041

Kickass tutorial, very well explained.


All times are Eastern Standard Time (GMT -5) | Current Time: 08:20 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!