WombatNation
YaBB Newbies
Offline
Posts: 15
Re: Array vs ArrayList. what's the diff?
Reply #1 - Jan 2nd , 2009, 6:34pm
For most users, IMHO, the main reason to use an array is if you are using an existing API that either takes or returns an array. Otherwise, use a Java Collections class like ArrayList or LinkedList. Even if you need to call a method that takes an array, you can do all your work with an ArrayList and then use the toArray() method on the ArrayList to get the contents as an array. ArrayList is a class that is part of a larger group of classes called the Java Collections classes. You can find them in the package java.util. Here's the link to the Java 1.4 overview for the Collections classes. http://java.sun.com/j2se/1.4.2/docs/guide/collections/overview.html Processing supports only the Java 1.4 API, though it runs fine on later versions of Java. There were a lot of additions to the Collections interfaces and classes in Java 5 and Java 6, so you should stick to the Java 1.4 docs to avoid confusion. There are a few classes in java.util that are old implementations (e.g., Hashtable, Stack, Vector) that should generally be avoided. Use a HashMap instead of a Hashtable and an ArrayList instead of a Vector. The new classes have more functionality and are faster. If you need multi-threaded support, you can synchronize on the object, or even better, use Collections.SynchronizedList(list) or Collections.SynchronizedMap(map). One of the biggest differences is that Java Collection classes like the ArrayList support dynamic resizing. Many of them use arrays for their internal implementation. However, the Collection classes add a lot of useful methods. Processing helps out a bit with the append() method. It acts very much like the add() method for an ArrayList, in that it hides the resizing of the array for you. However, I doubt (note to self, download source for Processing today) that it is as efficient if repeatedly called. When you initialize an ArrayList (e.g., ArrayList a = new ArrayList()), an internal array is created. The JVM implementation determines the size. My experience is that the Sun JVM uses 10 for the initial capacity. When you call add() the first time, the ArrayLists sets the object you pass it into the first slot in its internal array. And so on, until it needs to resize the array, which it does by creating a new array and copying the contents of the old array. I'm guessing the Processing append() method is allocating a new array and copying contents each time it is called. In earlier Java runtimes, arrays could be considerably quicker than Java Collections classes when setting values into existing array slots. However, Sun has done a great job of closing the performance gap to the point that it is now negligible. The Java Collections classes also provide implementations of many different algorithms (though, not so much in Java 1.4). For example, the LinkedList performs extremely well if you need to insert or remove elements from the middle of a list.