I am curious as to why Arrays are used when you could use an ArrayLists instead? Wouldn't it always be better to use an ArrayList?
Check out this comparison.
As you can see, there are important differences between the two constructs. You'll find APIs using one or the other (or both), and you have to understand the pros/cons and the functional differences between the two.
One particular difference is that a native array can store primitives without the inefficiencies of boxing/unboxing. That's significant when you have sizeable arrays representing data streams / data sets.
Note also that an ArrayList
is not covariant. That is, an Integer[]
is a Number[]
, but an ArrayList<Integer>
is not a ArrayList<Number>
. See here for more details.
Arrays and ArrayList
s serve different, though sometimes overlapping, purposes.
But aside from that, the simple fact is that you don't code in a vacuum, and so you're going to use APIs that involve arrays, so learning about them isn't optional. Arrays are an absolutely fundamental structure in computer science.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With