Lesson 12

Arrays

Author: Brandon Fajardo (11/10/17). Updated (8/7/20)

Introduction

Review: In the last lesson, we talked about user input.

Lesson topic: In this lesson, we will be going over arrays, a way to store a collection of values.

Topic 1 - Array

Subtopic 1 - Definition

Array: an object which holds multiple values within a single list. Each item in an array is called an element.

Index: a number associated with each element. Each element is referenced in the array by its index.

Subtopic 2 - Declaring

Arrays are declared with the following notation:

data type[] variable;
// or
data type variable[];

Subtopic 3 - Assignment

Arrays are assigned using the following notation:

variable = new data type[n];

*The data type must match in both the declaration and assignment.

n is replaced with the number of elements you wish to hold in the array. This must be a natural number [1, 2, 3, … n).

Subtopic 4 - Simultaneous declaration and assignment

Declaration and assignment can be written together:

data type[] variable = new data type[n];
// or
data type variable [] = new data type[n];

Note:

  • You may have noticed that creating an array is similar to creating a variable.

Topic 2 - Referencing

Subtopic 1 - Zero-indexed

As mentioned above, to reference each element in the array, its index is used.

Arrays in Java are zero-indexed, meaning the first element of an array has an index of 0 and each subsequent element has an index corresponding to the next whole number [0, 1, 2, 3, …]

This means that each element will be referenced by an index which is one less than what one might expect its natural ordering to be.

Element 1 has an index of 0, element 2 has an index of 1, etc.

In short: element n has an index of n-1.

Subtopic 2 - Accessing elements

To access an element in an array by its index, we use the following notation: variable[i] where i is just the element’s index

EX:

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

Referencing an index which is not defined produces the error, ArrayIndexOutOfBoundsException.

Subtopic 3 - Assigning values

What we now want to do is populate our array with values. You can assign a value for an element in an array, using:

variable[i] = value;

When an array is initially created, it is populated with default values. The default value itself will depend on the data type of that array. This value will often be 0 (int, double) or the special null value (String), which represents a lack of information.

Example

String[] array = new String[3];
array[0] = “My first element”;
array[1] = “Any string can be written”;
array[2] = “Test”;
array[3] => This would cause an error

Topic 3 - Curly Bracket Notation

We may also populate an array with values all at once. We can do this by assigning the values in a comma-separated series inside a set of curly brackets.

The notation is as follows:

data type[] variable = {e1, e2, e3, e4, ...};
// or
data type variable[] = {e1, e2, e3, e4, ...};

EX:

String[] example = {“Hello”, “World”);

These elements are indexed according to the order they appear in the curly brackets.

In this case, “Hello” would be referenced by example[0]. “World” by example[1].

Topic 4 - For Statements and Arrays

With smaller arrays, it can be easy to individually access each element by its index. But at some point, we may want to store a large number of elements, such as 1,000s of values. Individually accessing each one is simply inefficient.

We can speed up the process by using a for loop. By accessing each element in an array using variable[i], and then iterating through i in a for loop, we can quickly go through all of the elements in the array.

for (int i = 0; i < variable.length; i++) {
    variable[i] = value;
}

Here, variable.length is a special property of an array, and is equal to the length of the array (the number of elements).

Example:

for (int i = 0;  i < array.length; i++) {
    array[i] *= 10;
}

This goes through an array and multiplies each number by 10.

Topic 5 - Multidimensional Arrays

There are also such things as multidimensional arrays. These are arrays that hold multiple dimensions of values. For example, a 2-dimensional array is similar to how an excel spreadsheet holds information.

Accessing in this type of array is achieved by using multiple sets of accessing brackets: variable[i1][i2]

Read more about this concept here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Conclusion

Recap: In this lesson, we learned about arrays and what they can do. We learned about how to create an array, how to access the values within an array, and how to populate an array. Furthermore, we learned about an efficient way to iterate through the values in an array. Finally, we learned about the potential to have multidimensional arrays.

Next: In the next lesson we will be learning about ArrayLists.

Last updated