Arrays vs ArrayList

When we have arrays in java, why did they provide arraylist? What is the basic advantage of ArrayList over Array?Thanks in advance
[144 byte] By [srisria] at [2007-10-3 4:57:31]
# 1
ArrayList automatically grows.ArrayList fulfills the List contract, so it can used interchangably with other Lists when code is written to use the interface.
paulcwa at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 2

ArrayList (and most other List implementations) are easier to handle, since they grow on demand and don't require you to reallocate anything by hand.

Additionally they cooperate better with Generics than simple arrays do.

You should really google for a tutorial on the Collections framework and get to learn and love to power.

JoachimSauera at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 3
Thank you very much for the information. Yeah i just now look at the generics and it is very powerful. I love that concept...
srisria at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 4

> When we have arrays in java, why did they provide

> arraylist? What is the basic advantage of ArrayList

> over Array?

>

> Thanks in advance

Look at the API that would answer your question. All the functionality available for Arraylist is it available for arrays as well.

It is very similar to asking why have String class when you can have an array of chars.

Sun is just trying to make your life easy

kilyasa at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 5
ArrayLists can't contain holes. So when you iterate over them, you don't have to check for null references.Imo there are very, VERY few cases where you actually need arrays. In most cases, ArrayLists are far more intuitive, robust and flexible.
Mongera at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 6
> ArrayLists can't contain holes. So when you iterate> over them, you don't have to check for null> references.Check that. ArrayLists can contain null elements, per the JavaDocs.... and permits all elements, including null...
warnerjaa at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 7
> When we have arrays in java, why did they provide> arraylist? What is the basic advantage of ArrayList> over Array?Read the ArrayList API docs. Ask yourself how you would do each of those things with an array.
jverda at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...
# 8

> When we have arrays in java, why did they provide

> arraylist? What is the basic advantage of ArrayList

> over Array?

Arrays are for storage. ArrayList provides functions/API that you don't get with just an Array. So it's more than storage. With ArrayList you get storage and built in functionality with it. ArrayLists of course use arrays under the hood to store data.

SoulTech2012a at 2007-7-14 23:02:49 > top of Java-index,Java Essentials,Java Programming...