Arrays – A Ray of Sunshine for your Programs

Arrays are a foundational data structure in many programming languages, especially those that come from the same genre as c++.  However, different languages treat arrays different, meaning that they have different “features”.

Reading: Overland Chapter 6 – sections 6.1 (6.1.1 only) and 6.2.

Why, why, why? Why do we need arrays?

This is a great question.  You’ve likely covered this in another language before, but I’ll review some of the motivations here.

Consider the scenario of writing a simple program to manage test grades for a middle school teacher.  If the class has 30 students, to store all of the test grades for one exam, you’d need 30 individual variables (not counting any that you’d need for basic statistics such as mean and standard deviation).  Summing the scores would become an unwieldy adventure in both ensuring you have all of the correct variables accounted for as well as making sure that you don’t accidentally put a ‘-‘ where you meant to put a ‘+’.  Extend this scenario to the case where you might be in a class with 500 of your closest friends taking an outrageously interesting class…perhaps an advanced c++ class!  Quickly, the notion of maintaining an individual variable for every piece of data becomes ludicrous.

So, programming languages such as C++ provide a mechanism to store a “collection” of data that is accessible with a single identifier (the array name) and some type of numerical offset. Typically (and this is certainly true for C++ as well as Java), the basic, 1-dimensional array concept can be extended to allow for arrays of multiple dimensions.  For example, you can think of a 2D array in the grade book example above as a data structure to store the test scores for all students for more than 1 exam.  We’ll get into multidimensional arrays soon.

Differences between Java and C++ arrays

There are some big differences between how arrays are handled in Java and in C++.  Not paying attention to them can cause some serious headaches – so watch out.

  1. Object vs. Primitive
    • Java: Arrays are objects.  That means that they can be “queried” for information.  Probably the most used aspect of array objects in Java is to query them for their length (perhaps in a for loop).  You probably are also familiar with the idea of the ArrayOutOfBounds exception that is thrown when you try to access an array element that doesn’t actually exist.
    • C++: Arrays are not objects; they are more primitive structures to the language.  Since they’re not objects, you can’t ask them for their length.  They won’t throw an exception if you try to go outside their bounds.  These are the things that cause headaches.
  2. Allocation
    • Java: Since arrays are objects, they are allocated at runtime (using the new operator).  Remember seeing something like int [] data = new int[20]; in your code?
    • C++: Arrays can be allocated at compile-time.  You may see something like int data[20]; in c++.  That is completely legal.  It is possible to allocate arrays at runtime as well in C++, but we’ll get to that a little later.
  3. Bounds Checking
    • Java: An array will not allow you as the programmer to go past its bounds.  If you allocate 10 elements, you can’t then attempt to access the 12 location – Java will throw a run-time exception and shut you down!
    • C++: Remember, arrays in C++ aren’t objects – they’re primitives.  So, if you allocate 10 elements and then attempt to access the 12 element (which doesn’t actually exist), nothing will stop you.  You may think, “wow, that’s dumb.”  But, what you give up in protection you gain in a certain increase in efficiency.  Also, don’t fret – there are Java-like array containers in C++.  But, we need to understand the basics first before we learn the library containers.

Here are some YouTube videos to cover some of the syntax and other explanations.  Video 01, Video 02, Video 03.

Speak Your Mind



This site uses Akismet to reduce spam. Learn how your comment data is processed.