Lesson 4 - Array
Objectives
Whats is an array? Declaring an array? Initialization
Array operations – using “for” loop with
an array
Array, why do we care?
The car example from the previous
chapter on object-oriented
programming.
You may remember we developing a
program that contained multiple instances of a class, that is, two object.
Array, why do we care?
How could I take this further and write a program with 100 car objects?
We could try some ways, perhaps, with some
clever copying and pasting, you might write a program with the following begining:
Car myCar1; Car myCar 2; Car myCar 3; ....
Array, why do we care?
An array will allow us to take these 100 lines of code and put them into one line.
Instead of having 100 variables, an array is one thing that contains a list of variables.
What is an array?
From the Chapter 4, you may recall that a variable is a named pointer to a location in memory where data is stored.
In other words, variables allow programs to keep track of information over a period of time.
An array is exactly the same, only instead of pointing to one singular piece of information, an array points to multiple pieces.
What is an array?
What is an array?
You can think of an array as a list of variables. A list, it should be noted, is useful for two important reasons.
a. Number one, the list keeps track of the elements in the list themselves.
b. Number two, the list keeps track of the order of those elements (which element is the frst in the list, the second, the third, etc).
What is an array?
In an array, each element of the list has a unique index, an integer value that designates its position in the list (element #1, element #2, etc).
In all cases, the name of the array refers to the list as a whole, while each element is accessed via its position.
What is an array?
Declaring and Creating
Array
In Chapter Variables, we learned that all
variables must have a name and a data type.
Arrays are no diferent.
The declaration statement, however,
does look diferent.
We denote the use of an array by placing
Declaring and Creating
Array
For example, we started with an array of
primitive values, for example, integers.
Declaring and Creating
Array
The declaration in Figure 9.3 indicates that
“arrayOfInts” will store a list of integers.
The array name “arrayOfInts” can be
absolutely anything you want it to be (we only include the word “array” here to illustrate what we are learning).
One fundamental property of arrays,
however, is that they are of fxed.
One we defne the size for an array it can
Declaring and Creating
Array
Declaring and Creating
Array
The Array declaration in Figure 9.4 allow us to specify the array size: how many elements we want the array to hold (or, technically, how much memory in the computer we are asking for store our beloved data).
Declaring and Creating
Array
Example 9-1: Additional array
declaration and creation examples!
a. Float [] scores = new foat [4];
b. Human [] people = new Human [100];
c. Int num = 50;
Car [] cars = new Car[num];
d. Spaceship[] ships = new
Declaring and Creating
Array
Exercise 9-1: Write the declaration
Declaring and Creating
Array
Answer of Exercise 9-1:
Int [] a = new int [30];
Declaring and Creating
Array
Exercise 9-2: Which of the following
Declaring and Creating
Array
Answer of Exercise 9-2:
VALID
No VALID
VALID
VALID
Initializing an Array
One way to fll an array is to hard-code
the values stored in each spot the array.
Example 9-2: Initializing the
Initializing an Array
As you can see, we refere to each
element of the array individually by specifying an index, starting at 0.
The syntax for this is the name of
Initializing an Array
A second option for initializing an array
is to manually type out a list of values enclosed in curly braces and separated by commas.
Example 9-3: initializing the
Array Operations
Consider, for a moment, the following
problem:
(A) Create an array of 1000 foattng
potnt numbers.
(B) Intttaltze every element of that
Array Operations
Part A we already know how to do.
what we want to avoid is having to do
Array Operations
Let’s describe in English what we want to
program:
For every number n from 0 to 99,
Array Operations
Array Operations
Unfortunately, the situation has not
improved.
We have, nonetheles, taken a big leap
Array Operations
Example 9-4: Using a while loop to
Array Operations
A for loop allows us to be event more concise,
as example 9-5 shows.
Example 9-5: Using a for loop to initialize
all elements of an array
What was once 1000 lines of code is now
Array Operations
We can exploit the same technique for
Array Operations
For example, we could the array and
Array Operations
There is one problem with Example 9-6:
the use of the hard-coded value 1000. Striving to be better programmers, we should always question the existence of a hard-coded number.
In this case, what if we wanted to change
the array to have 2000 elements?
If our program was very long with many
Array Operations
Fortunately for us, Processtng gives
us a nice means for accessing the size of an array dynamically, using the dot syntax we learned for Objects in Objects.
Lengthis a property of every array and
Array Operations
Let’s use length while clearing an
Array Operations
Exercise 9-6: Assuming an array of
10 integers, thats is
Write code to perform the following
Array Operations
Exercise 9-6: Assuming an array of
10 integers, thats is
For (int i = 0; i < nums.length; i++){
Nums[i] = nums[i] * nums[i];
Array Operations
Exercise 9-6: Assuming an array of
10 integers, thats is
For (int i = 0; i < nums.length; i++){
Nums[i] += int(random(0,10));
Array Operations
Exercise 9-6: Assuming an array of
10 integers, thats is
For (int i = 0; i < nums.length-1; i++){
Nums[i] += nums[i];
Array Operations
Int total = 0;
For (int i = 0; i < nums.length; i++){
total+= nums[i];
Simple Array Example: The
Snake
Example 9-8: A snake following the
Simple Array Example: The
Snake
Example 9-8: A snake following the
Simple Array Example: The
Snake
Example 9-8: A snake following the
Simple Array Example: The
Snake
Example 9-8: A snake following the
Simple Array Example: The
Snake
Example 9-8: A snake following the