[ACCEPTED]-Why is it preferred to use Lists instead of Arrays in Java?-data-structures

Accepted answer
Score: 18

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

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

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

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

Score: 6

From Array vs ArrayList

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

Score: 3

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

Score: 1

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

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

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

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

Score: 0

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

Score: 0

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

Score: 0

Yes, Declaring it as List is better than 14 ArrayList. why? the answer is code decoupling.

The main 13 reason to do this is to decouple you code from a specific implementation of the interface.

The rest of code 12 only knows that data is type of List and 11 hence you can switch between different implementations 10 of the list interface.

Assume you declare 9 it as an ArrayList and then you realize 8 that you should have used LinkedList, changing 7 it wouldn't be easy because there's no guarantee 6 that the rest of code doesn't make use of 5 methods specific to ArrayList.

But if you 4 declare it as List, they you can simply 3 change instance of List from,

List <Integer> list = new ArrayList<Integer>();

to

List <Integer> list = new LinkedList<Integer>();

and for sure 2 this will work cuz you've written code that 1 follow contract provided by list interface.

More Related questions