Many people and authors suggested to us to use list than array.
List <Integer> list = new ArrayList<Integer>();
lis开发者_Go百科t.addElement(1);
....
What it is the reason behind it?
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.
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.
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.
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.
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.
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.
Yes, Declaring it as List is better than ArrayList. why? the answer is code decoupling.
The main reason to do this is to decouple you code from a specific implementation of the interface.
The rest of code only knows that data is type of List and hence you can switch between different implementations of the list interface.
Assume you declare it as an ArrayList and then you realize that you should have used LinkedList, changing it wouldn't be easy because there's no guarantee that the rest of code doesn't make use of methods specific to ArrayList.
But if you declare it as List, they you can simply change instance of List from,
List <Integer> list = new ArrayList<Integer>();
to
List <Integer> list = new LinkedList<Integer>();
and for sure this will work cuz you've written code that follow contract provided by list interface.
精彩评论