Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it important to learn about Arrays when we have ArrayLists? [closed]

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?

like image 535
ngc Avatar asked Aug 06 '13 16:08

ngc


2 Answers

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.

like image 69
Brian Agnew Avatar answered Nov 13 '22 15:11

Brian Agnew


Arrays and ArrayLists 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.

like image 43
T.J. Crowder Avatar answered Nov 13 '22 16:11

T.J. Crowder