CDA - Arrays Lesson

Arrays 

Arrays Presentation

Click through the Arrays Presentation below.

When we create a rectangle or an ellipse, we have to create a unique variable to represent the x and y locations if we want to update the variable during the program. Each rectangle created needs a unique variable name so the computer will know which rectangle or which ellipse to update. What if we had 100 rectangles? We would need to create a unique variable to represent the x and y location for each one. That would be very time consuming and require a lot of code.      

An array is a type of data structure that has the ability to store multiple values of the same data type. A data structure is a systematic way of storing, organizing, and assessing large quantities of data.  

A data structure is an object that includes both the data and some operations used to manipulate the data. The data we have been storing has consisted of the primitive data types, int, double, float, and boolean. However, an array can store primitive data or objects. 

objects polo mini beetleIn a true object-oriented programming language such as Java, everything is contained in a class. A class is a program code template for creating objects.  We will work with the PImage class in the next lessons. An object represents things in the real world. It has a state and a behavior. For example, a car class can create car objects.  Each car can have attributes like color, model, amount of fuel, and maximum speed. This information would be stored in variables inside the object. They also have methods or actions they can perform.  A car can go forward, go backward, turn, refuel, and many other behaviors

An array knows information about itself such as what type of data is in the array, where things are located in the array, and the number of items in the array. The object created from the array can call functions or methods to access, manipulate or change the data that is stored.

In the Java programming language, an object is created with the new keyword. There are two different ways to create an array. Declare the type of array and how many elements it will store. The type of array can be any primitive data type or an object.  

<Type>[] nameOfArray = new <Type>[size];

 Declare the type of array and use an initializer list to assign the actual data to the array:

<Type> [] nameOfArray = {items separated by a comma};

twotypesarrays

  • Each value in the array is called an element.    
  • Each element is stored at an index location.
  • The first index location starts at 0.  
  • The length of the array is the number of elements in the array.  Start counting from 1.  
  • Therefore, the last element in the array is at index location length - 1.    

The array below has 5 elements starting at index location 0 and last index at 4.  

The array to the right has 5 elements starting at index location 0 and last index at 4.  

Creating and Adding Elements to the Array

You can use an initializer list to declare and initialize the array. If you create the array with the initializer list, you supply the values for the array at the time you create it.

double [ ] grades = {57, 75.5, 42, 85, 99.5};

When you do not use the initializer list, you declare the array and how many elements and initialize it to the number of elements it will hold. You later add the values to the array at each index location.  

double [ ] grades = new double[5];

grades[0] = 57;

grades[1] = 75.5;

grades[2] = 42;

grades[3] = 85;

grades[4] = 99.5;

grades[5] = 100;     ERROR!  There is no index 5. If you try to access an index location that does not exist you will get an index array index out of bound error.

Manipulating Values

Once elements have been added to the array, you can access and change the values in the list by referring to the index number of the element. 

For example, if we want to access the grade at index 0, we could create a variable to store the information. The variable must be the same type as the element inside the array.    

double first = grades[0];

You can also print out the information in a System.out.print statement.

System.out.println(grades[0]);

We can change the information at an index location using the same process as assigning the information.   

index 0 element92
index 1 element75.5
index 2 element 42
index 3 element 85
index 4 element 99.5

grades[0] = 92;

The element in the first index location is now 92.

You can perform calculations in the index location.

double result = grades[0] /2;   This will take the element at index 0 and divide it by 2

result =  (92/2);

result = 46 ;

double result2 = grades[4/2]   This will refer to the index location [4/2] which is [2]

result2 = grades[2];

Length of Array

The length of an array is a constant. Once the array is created the length of the array cannot be changed. We can access the length constant similar to the way we call a method.  

int len = grades.length;

The variable len will be assigned the value 4.

Accessing All Values in the Array

Often we want to manipulate all the values in an array in a similar way. For example, we may want to print out all the values in the list. In a grade array, we may want to sum up all the values to compute the average.

Because the size of an array is a constant, we will use a definite loop, or a for loop.

To print all the grades in our array we will use the following for loop:

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

{

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

Since I will loop 5 times:  0, 1, 2, 3, and 4 when we use grades[i], we are actually accessing grades[0], grades[1], grades[2], grades[3], grades[4].

If we wanted to average all the values in a grade book, we will need to use an accumulator variable. The following is an example of how we can accumulate the values in the array grades and then compute the average.

Here is our grade averaging example:

double [] grades = {92, 75.5, 42, 85, 99.5};

double sum = 0;           // sum is the accumulator variable

double average = 0;     // variable to store the average

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

{

sum += grades[i];  

}

average = sum/grades.length;

System.out.println(average);

[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION