9: arrays why? what is an array? declaring and creating an array initializing and array array...
TRANSCRIPT
Arrays
csc 233animation scripting
Lesson Four: More of the Same
9: Arrays Why? What is an Array? Declaring and Creating an Array Initializing and Array Array Operations Array Examples Arrays of Objects
2
What is an Array
Recall, a variable is a named pointer to a location in memory where data is stored.An array is exactly the same, only instead of pointing to one singular piece of information, an array points to multiple pieces( a list).
3
What is an Array
Think of arrays as a list of variables.Arrays keep track of:
1.) The elements in the list2.)The order of the elements in
the listThe order of elements is just as important as the information itself.The name of the array refers to the list as a whole, while each element is accessed via its position.
4
What is an Array
In an array, each element in that array has a unique index # which designates its position in the list
The index of the array starts at ZERO.Why Zero?
Because, the first element in the array is at zero distance from the beginning of the array. The
second element is one away from the beginning, thus its number is 1.
index 0 int 5index 1 int 13index 2 int 51
5
When to use an Array
Any time a program requires multiple instances of similar data, it might be time to use an array.For example, an array can be used to store:
The scores multiple players in a gameA selection of 10 colors in a design programA list of fish objects in an aquarium simulationThe days per month in a scheduling programImages used in a gallery show
6
What is an array?
A variable is a named location in memory.An array is a named set of locations in memory
A ‘list’ of variables
Each element of the list has a number called an ‘index’
Starts at 0!
7
How to Declare an array in 2 steps,(Step 1)
Create the array variable (with the name and type)Make a named ‘list’ with the following parts:
datatype Square Braces Array name semicolon
int [ ] arrayOfInts ;
You are ‘declaring’ thatThere is an array named arrayOfInts( or whatever you name that array)
The elements inside are of datatype intYou have not (YET) declared how many are in inside
Other Rules:Arrays can be declared anywhere you can declare a variableDo not use ‘reserved’ words or already used names
8
How to Create an array in two steps (Step 2)
Reserve memory for all of the elements in the list:
Array name Keyword Type Size semicolonarrayOfInts = new int [42] ;
You are reserving memory for:The array named arrayOfIntsNeeds storage for [42] elements the size of type intNow you (and the compiler) know how many are in insideYou cannot change the size after you declare it!
9
Array Of Ints[0] [1] [2] [3] [4] … [41]
int int int int int int
Declare and Create at the same time:Type Braces Array name Keyword Type Size semi
int [] arrayOfInts = new int [42] ;
You are ‘declaring’ thatThere is an array named arrayOfIntsThe elements inside are of type int
You are reserving memory for the array Needs storage for [42] elements the size of type int
Note that the size cannot be changed, so you should make enough room when you start!
How to declare and create an array in one step:
10
Exercise 9-3: Write the declarations for:
Both Steps 1 and 2 on the same line:
11
Exercise: What’s wrong with these?
May be valid, maybe not!
12
Hand Initializing an Arrayworks best for a very , very short list
13
int [] stuff = new int[3];stuff[0] = 8;stuff[1] = 3;stuff[2] = 1;orint [] stuff = { 8, 3, 1 };
Initializing arrayssee web example
14
Declare and initialize by handint[] die = new int[5];die[0] = (int)random(0,6);die[1] = (int)random(0,6);die[2] = (int)random(0,6);die[3] = (int)random(0,6);die[4] = (int)random(0,6);
VS. Using a while loopint n = 0; while (n < 5) { die[n] = random(0,6); n = n + 1;}
Initializing with for loop
Learning Processing: Slides by Don Smith 15
Declare and initialize by handint[] die = new int[5];die[0] = (int)random(0,6);die[1] = (int)random(0,6);die[2] = (int)random(0,6);die[3] = (int)random(0,6);die[4] = (int)random(0,6);
VS. Using a while loop
for (int n = 0; n < 5; n++ ) { die[n] = (int)random(0,6);}
Walking off the end of the array… A very common problem is trying to access an element in the array that doesn’t exist:
Remember that we start at element 0What is the index of the last element? ______What happens if we try to access element [10]?
Try it and see!
16
for (int i = 0; i <= 10; i++ ) { xpos[i] = 0;}
Exception in thread "Animation Thread" java.lang.ArrayIndexOutOfBoundsException: 10
at OutOfBounds.setup(OutOfBounds.java:21)
int[] xpos = new int[10];
Additional Processing Array features
Processing provides a set of functions to help manage arrays shorten() – shortens array by 1
concat() puts two arrays togethersubset() takes smaller sections of one array and places them in another
append()expands an array by one elementsplice() inserts elements into another arrayexpand() – Make an array larger in size! default doubles sizesort() – Sort all the elements in the arrayreverse() – Reverse the order of all elements
17
Arrays of Objects
Using arrays to store multiple objects is a very powerful programming featurewe will create an array image objects as well as a “nervous point” object
Learning Processing: Slides by Don Smith 18
Summary
Arrays make it easier to store ‘groups’ or ‘lists’1) Must be exactly the same type inside2) Can be any length, but they do have an end!3) Each item has a index number (starting from 0)
Setting up arrays is a two step process:1) Declare the array (set name, type inside)
2) Create the array (sets length, uses ‘new’)Can be done on one line though!
Arrays can be initialized: { 1, 17, 28 }For loops and arrays are natural partners
Initializing all elements‘Walking’ through the array
Access array elements with [index]
19
Lab Time
Practice what you have just learned by creating an array of images of your own choice. Use loops to arrange these images within your canvas.
Create a button class that can take the following arguments:1. width2. height3. x location4. Y location5 . color6. add any ornamental element you like in addition
Interactivity: 1. the button needs to change color when rolled over2. the buttons need to index through the image array
Due next class20