Comp 110 Arrays

Native Arrays

An array is a container object that holds a fixed number of values of a single type. Arrays are also an example of a complex data type, and made up of a series of simple data types. When you create an array you also declare its size, which cannot be changed after creation. 

Each item in an array is called an element, and each element is accessed by its numerical index. Note: Arrays are zero indexed, so rather than counting indexes as: (1, 2, 3, 4, ....) you have to count indexes as: (0, 1, 2, 3). 

Let's take a look at an example of a simple array.

String[] hintonJames;//Declaration
hintonJames = new String[5];//Instantiation
String[] anotherArray = new String[5];//Both together

Notice that this syntax is not too different from declaring a normal string. First off, String[] is the value type of a one-dimensional array of strings. The name comes after (hintonJames, anotherArray). Then, when instantiating the array, you must use the new keyword, along with String[length], length being an integer that defines how many elements an array can hold.

Take a look at a visual representation, and see if it's a bit easier to visualize.

An array is like a hall-style dorm room (of all single rooms.) Let's make fill up hintonJames with some students.

String[] hintonJames = new String[5];
hintonJames[0] = "John Doe";
hintonJames[1] = "Jane Smith";
hintonJames[2] = "Kris Jordan";
hintonJames[3] = "Marcus Paige";
hintonJames[4] = "DJ Khaled";

So, other than the diverse population of Hinton James, what else is interesting about this bit of code, and what is printed out to the console?

So, what happens if we try and access hintonJames[5]? Hint: We would get an ArrayIndexOutOfBounds Exception. Though we instantiated the array with a length of five (new String[5]), arrays are zero indexed. Therefore, the five indices are 0, 1, 2, 3, and 4.

Arrays can only store one type of values. Though you can have an array of many values (up to 2,147,483,647), they all have to be of the same type, be it int, String, double, boolean, etc.

Easily accessing all values in an array / when you don't know the size of an array.

Let's say you have a method that adds up all the numbers inside of an array, that is given to you as an argument in your method. If you knew how many elements there would be in this array, then you could hard code it:

public int addNumbers(int[] numbers){//Length of 3
    return numbers[0] + numbers[1] + numbers[2];

However, what happens if we have an array of integers that is more than three values, or less than three values, or any arbitrary number? This is where loops can be very useful for traversing arrays. Let's look at an example with a while loop.

public int addNumbers(int[] numbers){//length is unknown
    int sum = 0;
    int i = 0;
    while(i < numbers.length){
        sum = sum + numbers[i];

So, let's move through that block of code line by line. So, first we declare an integer that can hold the values, which is our sum. Then, we declare an integer i that is used as our counter. The while loop, which loops while our counter, i , is less than the length of the numbers array. This solution allows the length to be any number, as the loop will iterate as many times as necessary. 

numbers.length looks like a method call, but it is no mistake that the parenthesis are omitted. Rather than calling a method, .length retrieves a public field, which is an integer equal to the number of elements in the array.

So, while i is less than the length of the array, we are making the sum equal to itself plus the element at the index i in the array. We use the counter variable to access the element in the array, since we are going to loop through every number until it is the last element in the array. So if we have an array:

int[] threeNumbers = new int[]{5, 6, 7};
int sumOfThreeNumbers = addNumbers(threeNumbers);

What value is sumOfThreeNumbers holding? (18). The declaration above is another way you can instantiate an array if you know the values beforehand.

To sum it all up, an array is a container of values of the same type, that contain a certain number of elements that are accessed by an index. Arrays are very useful in programming, where instead of trying to remember 100 different variable names, you can simply put them all in one array and search through the array later in your code.

A Review: 
  • Arrays are named variables that hold many values, called elements.
  • Each element is addressed by an index number and the array name (Format: arrayName[index number]).
  • All elements in an array have to be the same type and cannot be resized after they are created.
  • Array indexes start at 0, not 1 (i.e. the third element in an array has an index of 2).