We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Array vs ArrayList. what's the diff
Page Index Toggle Pages: 1
Array vs ArrayList. what's the diff? (Read 3091 times)
Array vs ArrayList. what's the diff?
Jan 2nd, 2009, 10:16am
 
so i've been through the chapter on arrays in shiffman's book, then notice that he's used something called ArrayList in his 'flocking' sample code. What's the general difference, and is there any huge advantage to one or the other? ArrayList seems easier to use than Array so i'm inclined to follow that path.
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.
Re: Array vs ArrayList. what's the diff?
Reply #2 - Jan 2nd, 2009, 11:55pm
 
thanks for the excellent explanation, i'm going to copy/paste your response for safekeeping! and i'll stick with ArrayList for now.
Re: Array vs ArrayList. what's the diff?
Reply #3 - Jan 3rd, 2009, 4:39pm
 
WombatNation, your explanations are excellent, indeed, and you are right about the Array methods of Processing: a new array is created and the old is copied each time .add() is called...

scntfc, don't hesitate to use simple arrays each time you handle a collection of fixed, known length data. Their access time is excellent and they use less memory, particularly for primitive types (int, float, boolean...). Plus you don't have to cast on access.
Re: Array vs ArrayList. what's the diff?
Reply #4 - Jan 3rd, 2009, 8:31pm
 
PhiLho  wrote on Jan 3rd, 2009, 4:39pm:
scntfc, don't hesitate to use simple arrays each time you handle a collection of fixed, known length data. Their access time is excellent and they use less memory, particularly for primitive types (int, float, boolean...). Plus you don't have to cast on access.


sounds good, i'll keep it in mind as i move on. i've been modifying shiffman's flocking sample code:
http://www.vimeo.com/2576145
and it started with ArrayList (i'm assuming for the ease of adding boids with mouse clicks). it might be a good exercise for me to switch the ArrayList out with an Array. thanks again!
Re: Array vs ArrayList. what's the diff?
Reply #5 - Jan 4th, 2009, 4:36am
 
PhiLho  wrote on Jan 3rd, 2009, 4:39pm:
scntfc, don't hesitate to use simple arrays each time you handle a collection of fixed, known length data. Their access time is excellent and they use less memory, particularly for primitive types (int, float, boolean...). Plus you don't have to cast on access.


Very good points. Without Java 5 generics, you always need to cast the return from get(). Also, without Java 5 autoboxing of primitive types, you have to do the conversions yourself.
Page Index Toggle Pages: 1