1

Possible Duplicate:
Array or List in Java. Which is faster?

Is it much more expensive to use ArrayList than a simple array in Java? I have lots of ArrayList and in some places I can use array, I am not sure if I should go through the trouble of replacing them all.

Concretely, I can imagine get for ArrayList is more expensive than using the operator [] for arrays.

Thanks

3
  • 2
    Is ArrayList manipulation the performance bottleneck of your application? If not, I wouldn't worry about it. Commented Jan 6, 2013 at 2:13
  • And those ArrayLists are filled with primitive types or objects created by you? I would keep ArrayList... Commented Jan 6, 2013 at 2:13
  • 1
    get() is O(1) for ArrayList, see stackoverflow.com/questions/2182597/… Commented Jan 6, 2013 at 2:17

4 Answers 4

3

It's a teeny bit more expensive. If your data isn't fixed size, and you do anything other than iterating over it, it's worth using ArrayList for the added readability and simplicity.

The one exception is for primitive types, for which primitive arrays are more likely to win.

Sign up to request clarification or add additional context in comments.

Comments

2

Donald Knuth made the following two statements on optimization:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" (From Wikipedia)

In other words, unless you have some benchmarks that indicate that you need to use a Java array then use the one that benefits you as a developer; which, in my opinion, is the ArrayList.

Comments

2

Generally speaking, if you need to do stuff that's really time-sensitive, the use of ArrayList versus Object[] is not going to be where your bottleneck is. Database queries, network communication, disk access, and un-optimized arrays and such are going to be far worse.

Since ArrayList is not synchronized, the performance is generally not going to be substantially worse than Object[], particularly if you specify the size before adding elements.

Comments

2

A very high percentage of the cost of an application is its design, development and maintenance. By comparison, the cost of running the application is usually so much smaller it is not worth worrying about. So when you think about efficiency, you should think about your efficiency and those who have to maintain your application after you (assuming your application will be useful ;)

Say you have application where performance is an issue. A common mistake is to just guess where the performance issue are and "optimise" those. The problem with this approach is that it usually

  • adds complexity
  • reduces maintainability.
  • misses the biggest performance bottlenecks
  • can make little or no difference or can even make performance worse. (The last time some one tried to optimise some code I had written it was 50% slower ;)

What you need to do is to perform some realistic tests and measure the performance bottlenecks which exceed your specific measurable business requirements A vague go-as-fast-as-possible is not specific or measurable, and usually non-sense in my experience.

Once you have made the changes to the key bottlenecks you need to re-test the system to ensure it really helped.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.