4

Both ArrayLists and Vectors make use of typical arrays internally. However, that leaves me thinking... why would I use ArrayLists when I can technically do the same thing using Arrays? Is convenience the only reason? Do performance-critical applications ever make use of an ArrayList?

Any tips would be appreciated.

4 Answers 4

3

I believe there are multiple reasons to prefer Lists over "implementing lists over arrays" or over "using arrays", but here are the two that I think are most important:

  1. Lists have better support to generics than Arrays (you can, and should, read about it in "Effective Java" by Bloch - see Item 25)
  2. If you ask about using ArrayList vs. implementing it yourself - I find it hard to believe that you'll do a better job than the guys that developed it in openjdk (Josh Bloch and Neal Gafter).
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you alfasin. I definitely agree with your second point. But can you elaborate on the first part of your answer... are you saying that lists should be used as an alternative to arraylists?
@Grateful no, that's not what I'm saying. I'm saying that List/ArrayList should be preferred over: 1. Array. 2. implementing a List by yourself.
This is useful information. So I am sorry for being pedantic, but you seem to be using the terms lists & arraylists interchangeably... and that is confusing to me.
@Grateful that's perfectly fine to ask for a clarification. I mentioned ArrayList only because you did as well in your question - otherwise I would generalize and use "List" only.
Okay. Kindly correct me if I have misunderstood this, but I think you are basically saying that ArrayLists should be preferred over Arrays. And that Lists as a whole (which includes ArrayLists) are a better match for generic types than Arrays.
|
2

Yes, performance critical applications use ArrayList all the time. It's very unlikely that array access is the dominant factor in the vast majority of programs written in Java.

The ArrayList Collection interface is much richer than the functionality provided by built-in primitive arrays. This extra functionality will save you development time as well as debugging time by not having to write those algorithms yourself.

Additionally, many programmers are already familiar with the ArrayList Collection interface and thus by utilizing the existing standard libraries it will make your code easier to read and maintain for the long term.

2 Comments

Okay, I can definitely see the convenience. However, I am still wondering whether there are any alternatives that are smarter about being "dynamic"... I am having a hard time coming to terms with performance-critical applications using something that only appears to be dynamic but really isn't deep down.
Huh? Don't try to make arguments about performance without actually profiling your code. Unless you've actually shown that array access is a critical bottleneck, you will only damage maintenance of the code with no actual benefit.
0

One reason is that ArrayLists sizes are dynamic, arrays aren't.

5 Comments

Perhaps I wasn't clear enough. I am aware that arraylists are "dynamic", but the point is that they are internally using a regular array to achieve this. Why shouldn't I just simulate this dynamic behaviour myself?
There is no need to reinvent the wheel.
Sure, you can always write your own version of ArrayList that uses an array under the covers, but there's no point to it. That's already been done.
Okay, I can't argue against that... but are there any alternatives out there? Doesn't Java provide something that handles dynamic array behaviour in a better way?
No, ArrayList is the state of the art dynamic array collection built-in to Java.
0

The internal implementation of ArrayList is array only. but ArrayList is an wrapper class which is having more capabilities added to it. These capabilities are not available when you deal with Array directly.

For example,

Delete an element from array, you will have to implement logic if your are using an Array. But if you are using ArrayList, it will do the deletion for you.

Adding an element to array: If you are using an array, you will have to implement the logic. But using an ArrayList, it is pretty easy.

You will find lot of methods in this ArrayList class that are handy for day to day use.

Hope this will help you.

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.