1

I have gone through many codebases on Github and almost everywhere I found one common pattern to use Arrays instead of Lists.

example :

Class Attributes {
   ...
   ...
   Attribute[] attribute;
   ...
   ...
}

So My question here is, Why do most of these projects are using raw arrays and not collections? The below code is still the same functionality wise and using arrays require most of the code to be re-written to handle expansion of array.

Class Attributes {
   ...
   ...
   List<Attribute> attribute;
   ...
   ...
}

EDIT 1: After a lot of searching I found out that arrays have lower memory footprint than Collections. So now the question arises, Why is this?

Well, the obvious answer seems because internally they use a lot of other variables to maintain the state and do a lot of other stuff. But isn't that required anyway, I mean if we want to store numbers and the size of array can increase or decrease decrease, we'll have to implement those methods anyway.

swayamraina
  • 2,958
  • 26
  • 28

1 Answers1

1

I don't know your sources, but I'm pretty sure collections are used more often than arrays, since they provide a dynamic structure. Yet there are good reasons to use arrays as well;

-If you have limited memory, using arrays is a better choice.

-If you have strict deadlines on your program, using arrays is a better choice.

-If you know the amount of elements to use in your program, using arrays is a better choice.

In a nutshell, if you need performance, or you have limited resources use arrays. If you need functionality, use collections.