Getting Started with Arrays: A Guide for New Programmers

If you’re just starting out with computer programming, you probably found this article by searching “how to use arrays” or something similar.  Arrays appear in nearly every programming language, and they’re an essential data structure for organizing and manipulating pieces of information.  In this article, we’ll talk about what arrays are, how to use arrays, and what arrays are used for in programs.

(By the way, the code examples in this article are written in Java, a strongly-typed, object-oriented programming language.)

What are arrays?

An array is a kind of “data structure”–a specific kind of format used for organizing, managing, and storing data in a computer application.   Arrays are a type of “collection” structure, meaning that they can contain multiple “elements” or pieces of information, and they’re used to group like pieces of information together. 

Let’s say, for example, that you were a teacher, and you had a list of your students’ names.  In code, you could define all of those names in separate variables: 

String james = “James”; 

String kate = “Kate”; 

String david = “David”; 

String joey = “Joey”;

And if you wanted to print all of the student names out to the console, you would have to write the same line of code four times, replacing the variable each time: 

System.out.println(james); 

System.out.println(kate); 

System.out.println(david); 

System.out.println(joey);

Programmers are lazy!  That’s too much work!  It’s too much typing!  No one will ever stand for it! 

Since you have a collection of the same type of data (names), it would make sense to put all four pieces of information into an array, so that the information is contained in a single piece of data, and then use a loop to print all the data in the array out to the console, one thing at a time: 

String[] nameArray = new String[] {“James”, “Kate”, “David”, “Joey”};

for(int i = 0; i < nameArray.length; i++) { 

    System.out.println(nameArray[i]); 

}

Don’t worry about the syntax just yet–the point of this explanation is to show that all four names are now part of a single array called “nameArray”. 

How do I create arrays?

The syntax for creating an array will change with your programming language.  In most languages, you’ll create an array by specifying its data type and name.  Depending on the language you’re using and the way you’re instantiating the array, you may also need to specify the array’s length and/or the starting collection of elements in the array.  While the examples in this article are in Java, remember that syntax may vary across languages, and you should research and read documentation concerning arrays for the specific language you’re using.

In most strongly-typed languages, arrays have a fixed length (determined at the time of their creation), and consist only of elements of the same data type.

How do I use arrays?

Each element in an array is assigned a numbered “index” that represents its location in the array.  You can think of an array like a file cabinet–each folder in the cabinet represents one item in the array, and the folders are put into the cabinet in a particular order.  Array indexes are unique (since there is only one piece of data per location), and they usually start at 0.

Why do arrays start at 0 instead of 1? 

Each byte of a computer’s memory has a unique “address”.  When an array is created, memory is set aside for the entire array (this is why arrays are fixed-length structures, and why you need to specify a length when creating it).  The memory set aside for an array when the array is instantiated is consecutive, with one block of memory for each element in the array.  

When an array element is accessed or retrieved, the computer calculates which block of memory holds that element based on the array’s base memory address, which is the starting location of the first element.  Starting at 0 means that the first element’s address will be the same as the array’s base location, the second element’s address will be the base location plus enough memory to hold one element, the third element’s address will be the base location plus enough memory to hold two elements, etc.  In other words, the address of a particular element will be the array’s starting location offset by however many blocks of memory are reserved for proceeding elements.  This is why you will sometimes hear the term “offset” used instead of “index”! 

Using the index

To access items in an array, you’ll need to use the index.  For example, this code will retrieve the third name in the array of names, and assign to the variable ‘name’: 

String name = nameArray[2]; //remember, arrays start at 0

//the value of ‘name’ is now “David”

You can also replace an item in an array, again by using the index: 

nameArray[2] = “Tony” //the value at index 2 in nameArray is now “Tony” 

Be careful not to try accessing or modifying an index that doesn’t exist, or else you’ll get an “index out of bounds” exception.  This exception is raised when you try to access an index that is past the end memory location of the array: 

nameArray[6] = “Mary”; //this will cause an exception to happen, because nameArray is only four elements long

Looping through arrays

One of the most common things to do with arrays and other collection structures is iterate, or loop, through them.  You can use a for loop with arrays to easily loop through the collection: 

int[] numArray = new int[] {1,2,3,4,5};

for(int i = 0; i < numArray.length; i++) { 

    System.out.print(numArray[i] + “ , “); 

}

// 1, 2, 3, 4, 5

By starting the loop at 0, and looping until the end of the array is reached, you can access each item in the array, in order.  In a for loop, you can adjust the starting location, the iterator, and the condition to access every other item, every third item, the first half of the array, etc.  Keep in mind that the length of the array will be equal to the last index plus one, so that you don’t accidentally cause an index-out-of-bounds exception!

Conclusion

As you continue on your programming journey, working with arrays will become second nature–they can be found in nearly every programming language and are a fundamental building block of software development.  It is valuable to become familiar with arrays before moving on to other, more complex data structures, and the skills you build practicing with arrays will serve you well as you move on to collections such as lists, maps, and queues.  Happy coding!

Add a Comment