views:

237

answers:

6

Many people and authors suggested to us to use list than array.

List <Integer> list = new ArrayList<Integer>();
list.addElement(1);
....

What it is the reason behind it?

+2  A: 

Lists can easily grow in size, and you can add and remove elements in the middle of the list easily. That cannot be done with arrays. You need to consider what you need the list for though. If you don't think the list is going to change a lot, then use an array instead.

Marius
Auto-resize is not the only reason though. List participates in all the Collections API goodness, so you get to solve most of your datastructure related problems by just using the API.
cherouvim
A: 

From Array vs ArrayList

An ArrayList is better than Array to use when you have no knowledge in advance about elements number. ArrayList are slower than Arrays. So, if you need efficiency try to use Arrays if possible.

astander
A: 

EDIT:
In certain cases when dealing with primitive types, its better to go with arrays because in the case of Arraylists, it involves boxing and unboxing of the primitives which might be a bit slower when compared to handling primitives with arrays.

Zaki
of course you can specify the element type of an array.
Ben Voigt
Please do not declare myList to a concrete type (ArrayList). It's best if you use the Interface List.
cherouvim
It doesn't matter if you use List or ArrayList if the field is not public API. Eg, on Android it is less work to call a method on the ArrayList concrete type than on the List interface. I tend to declare my private fields the concrete type and return the interface in the public API (getters, etc).
NateS
@Ben Voigt, yes ofcourse. my bad. edited appropriately.
Zaki
+1  A: 

You should generally prefer to choose the right data structure for the job. You need to understand your task at hand as well as all the different options you have and how they compare in terms of iteration, and searching, and adding, removing, and inserting data. In general, you need to understand how the data structure accesses and manipulates memory and choose the best data structure based on how you anticipate your application will be used.

Obviously, it isn't always clear-cut. But you can understand the ideals for different data structures.

For example, purely static, fixed length data in which you'll only iterate, without a need for search, is ideal for an array. It's common to use such arrays in cipher algorithms. If the data is static but instead of iterating, you need to search, you might want some type of tree structure. If you want fast insertion, hashing is probably the ideal. If the data changes often, you want a structure that is efficient at changing its size, like a list.

Of course, there's many variations and combinations of data structures designed to solve all kinds of specific problems. The reason there are so many is because of the importance they play in writing efficient programs. Anyway, my point is, learn about data structures. Understand the ideal situations for each and then you'll be able to decide or design suitable data structures for any task.

nicerobot
A: 

One thing to keep in mind is that the Java Collections classes favor general purpose ease of use over optimization for specific scenarios. So, as a previous responder said, you really need to consider how you're going to use it.

For example, if you're creating "large" data structures, then ArrayList can get pretty inefficient. Each time you hit the limit of the array, it allocates a new one at (I believe) 2x the size. So on average an ArrayList is only going to be 75% utilized.

In general one can consider the Java Collections to be first approximations that are usually good enough most of the time, and when you have measurable performance issues you should be prepared to use alternate, more specialized Collection implementations.

In the case you mention, you can consider ArrayList to be just a more convenient way to deal with an array.

Kashif Q.
A: 

I use Lists, ArrayLists et c mainly because I don't need to worry about where the next free slot is or if its large enough, since Sun already did that for me.

Ledhund