One reason that first-year programming classes use arrays is legacy: that's how the professors originally learned it before we started using standard libraries with dynamic lists baked in. Using the primitive data types is also more generally applicable: arrays exist in pretty much any computer language under the sun (and can be implemented in a handful of assembly instructions). When I was first learning programming, implementing a linked list was one of the assignments.
It's much easier to start from first-principles and then say "That's the basic structure. This language (or its libraries) gives you this high-level data structure that does all that, but gives you x, y, and z," than it is to say "So that's these high-level data structures, now here's what's under the hood." Learning to reason about whether to use a LinkedList vs. an ArrayList (or a HashSet vs. a TreeSet) is usually a second or third-year Algorithms course. Lists and Maps have the same interfaces and give the same results, but can have dramatically different behaviors in an application of any size. And once you get out of Programming 101, there's no guarantee that Programming 102 will be using the same language. If you start from the concept of an array, you can just say "this is how you use arrays in this language", rather than trying to explain why you don't get all the fancy things that Java Lists give you. And once you get out into "the real world", there are still people out there who have to program in C (particularly in high-performance projects where you need to get as close to the metal as possible).
Another reason to prefer "array" over "List" in an introductory course is that arrays are fundamentally easy to understand: An array of 20 bytes
takes 20 bytes (plus a couple to indicate either the end of the array or the length, depending on implementation).
A "List" is a completely different kettle of fish and can be implemented in many different ways (ArrayList, LinkedList, and probably a couple I've forgotten), with fundamentally different performance characteristics. Without understanding the guts of what the different List classes are doing, you can't have a meaningful discussion of when you should use List foo = new ArrayList()
vs. List foo = new LinkedList()
. If you try to get the student to use a List implementation, someone is going to ask why you're using ArrayList instead of one of the other implementations. And "ArrayList" includes the word "Array" and is backed by one, so it really isn't a huge logic jump from "array" to "ArrayList".
Contrary to popular belief, there ARE situations in which it makes sense to use arrays over Lists, particularly when you're dealing with a list of static size. Here are a couple:
- Lookup and iteration are slightly faster because you're not dealing with method invocation overhead:
foo[n]
dereferences and does some behind-the-scenes pointer arithmetic, while foo.get(n)
has to dereference, do a method invocation, do a second dereference, and then maybe do pointer arithmetic (if you're using an ArrayList; LinkedLists potentially need to iterates over every element of the List).
- Initialization is much cleaner:
int[] foo = new int[]{1, 2, 3, 4, 5}
vs. the suggestions in Another StackOverflow question