Most of the assignments in my school for the initial programming classes required me to use arrays. I work full time now, and I never used an array for any project that I have worked on. Even in the exisiting projects I never saw the use of arrays anywhere. In my opinion, List is easier to use and is a standard. Why do professors tell students to use arrays in their assignments? Is it just so that students understand the basics?
Since most of the universities teach Java, this question is specific to Java.
2
Because arrays teach concepts like indexing and bounds, two fundamentally important concepts in computer programming.
Lists are not a “standard.” There is a wide variety of problem spaces for which arrays are a perfect fit.
1
They probably wanted to start with the data structure that most accurately represents how a computer works, so you’re familiar with the fundamentals before they start introducing higher-level abstractions like Lists which make it easier to work with. Otherwise you’d have no way of understanding why a certain structure or algorithm is slow/fast for some operations and not others; if computer memory was actually made out of linked lists the world would be a very different place.
For much the same reason, my first programming class at college was in C, and the rest of the classes were all in C++, Java and other languages. It’s not that C is somehow better or easier, it’s that C (and Array) hides nothing from you.
13
The answers above are great, but I have another in mind. Java’s main()
method means that students encounter basic arrays very early on, often as soon as the first day of class. Why?
public static void main(String[] args)
It’s the first thing you’re going to have to deal with to write Hello World and beyond. (I’ve seen some courses use teaching IDEs like BlueJ at first, which allow you to point-and-click to run arbitrary methods, but we’ll set those aside…) While it might be worthwhile to handwave over some of these keywords for a little while, sooner or later most teachers are going to want to explain them. Indeed, a classic beginner-level test question is to ask students to give the meaning of each keyword in a basic Hello World program. And what do we find as part of our main method signature? An array (The reason for that is, in part, historical. ArrayList didn’t exist in Java 1.0). Arrays are part of that basic set of knowledge. List is not.
That said, it’s not that uncommon for classes to introduce ArrayList a little later into the course, especially once objects and their use have been covered. Even the AP Computer Science curriculum for Java includes ArrayList (I know it used to, and Google seems to indicate that it still does), although it ignores the fact that ArrayList implements List and the rest of the Collections Framework.
Finally, it’s my experience that university CS programs use Java as a means to exploring CS and programming concepts rather than to teach students how to become good Java developers. Some programs may be more focused on churning out professional developers while others focus more on theory, but in either case, there’s a lot to learn about how to use Java in real professional work that won’t be taught in most college curricula. This ranges from design patterns and techniques like those in Effective Java to frameworks like Spring, Hibernate, or JUnit, or even more common things like JSP or JDBC. With that philosophy in mind, emphasizing arrays over the more commonly used ArrayList makes a little more sense.
3
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, whilefoo.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
6
Java allows variables of any type to be stored into arrays. By contrast, ArrayList
only allows storage of references. One may thus not usefully discuss ArrayList
without first covering how auto-boxing will convert primitives to reference types, and how auto-unboxing will sometimes convert reference types to primitives:
for (int i=10; i<=10000; i*=10)
{
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(i);
l.add(i);
l.add(l.get(0));
System.out.print("i=" + i);
System.out.print(" #0==i:" + (l.get(0)==i));
System.out.print(" #1==i:" + (l.get(1)==i));
System.out.print(" #2==i:" + (l.get(2)==i));
System.out.print(" #0=#1:" + (l.get(0)==l.get(1)));
System.out.print(" #1=#2:" + (l.get(1)==l.get(2)));
System.out.println(" #0=#2:" + (l.get(0)==l.get(2)));
}
If the code had used an int[3]
rather than an ArrayList
, there would be no surprises. All three elements would compare equal to i
and to each other. Using ArrayList
, however, even though all three elements of the list will always compare equal to i
, and the first and third will always compare equal to each other, the first two elements will only equal each other when i
is 1, 10, or 100, but (on most implementations) not when i
is 1000 or 10000.
5
I think it makes sense to teach how to use arrays first due to the fact that ArrayList
uses an array internally. The ArrayList
class has a member variable called elementData
which is an Object
array.
From the JDK ArrayList
source code:
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer.
*/
private transient Object[] elementData;
When you add, update, retrieve or remove elements from an ArrayList
it uses this internal array to do those operations. As user Ixrec has already pointed out – ArrayList
is just a higher-level abstraction which is typically easier to work with.
2
Assuming that list are indeed easier to work with, as you say — that doesn’t really matter. Learning is more about “basic to complex” than “easy to hard”. If fundamentals weren’t important, then computer science wouldn’t be an academic field. You could just learn how to click together apps using existing frameworks/libraries from online tutorials. (Of course, someone has to write those libraries … and someone has to implement ArrayList
in the first place….)
1
It’s because the most crucial thing in academic education is to teach you to use the correct terminology to describe the things you do.
List is something other that array. and You can’t use java.util.List
in Java because it’s an interface. You usually use java.util.ArrayList
which being a List implementation, is not a list, but an object wrapper around dynamic array. So you say you use a ‘List’ but you use an array.
It’s very reasonable to skip that terminological chaos and simply use arrays to learn students what is array. If you use arrays in Java, at least you use arrays.
Honestly, it’s also the argument why teaching programming with Java isn’t a good idea. It’s hard to learn the basic concepts of programming correctly.
4
You’ve literally never seen or used any arrays at all? We use them all the time, in addition to lists. We usually don’t use Java, but we do use plenty of other languages that draw obvious similarities.
Between arrays and Lists, one’s lighter-weight and, additionally, more to-the-point, whereas the other’s got more functionality. As a general rule in programming, when there are two similar types that are basically divided along those lines, you should go for the lighter-weight one unless you actually need the fancier one. In addition to reducing overhead, this actually helps keep a handle on the amount of clutter and state in the program and particularly its code. If something goes wrong during testing, you have fewer places to look; and more importantly in the case of arrays vs. Lists, people get a better idea of the limited scope of what you’re actually using it for and trying to do with it.
And yeah, from an academic standpoint, there is the added reason of teaching students the basics. However this goes a little bit deeper. Arrays and Lists are a good example of bulkier types often just being built on top of, and frequently just wrapping around, underlying instances of the lighter-weight types. Even when Lists don’t have underlying arrays, they behave outwardly like they do. One part of teaching somebody what a List is would be to teach them what an array is.
I could see this maybe getting out of hand in a language like C++, where arrays basically could not be more stripped down than they are, but in higher-level languages, they’re almost Lists unto themselves. If they suit your needs perfectly in a given situation, why would you need to use something else?
1