Arrays animation scripting csc 233 Lesson Four More

  • Slides: 20
Download presentation
Arrays animation scripting csc 233

Arrays animation scripting csc 233

Lesson Four: More of the Same 9: Arrays Why? What is an Array? Declaring

Lesson Four: More of the Same 9: Arrays Why? What is an Array? Declaring and Creating an Array Initializing and Array Operations Array Examples Arrays of Objects 2

What is an Array Recall, a variable is a named pointer to a location

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

What is an Array Think of arrays as a list of variables. Arrays keep track of: 1. ) The elements in the list 2. )The order of the elements in the list The 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

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 5 index 1 int 13 index 2 int 51 5

When to use an Array Any time a program requires multiple instances of similar

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 game A selection of 10 colors in a design program A list of fish objects in an aquarium simulation The days per month in a scheduling program Images used in a gallery show 6

What is an array? A variable is a named location in memory. An array

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

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 int [ ] Array name array. Of. Ints semicolon ; You are ‘declaring’ that There is an array named array. Of. Ints( or whatever The elements inside are of datatype int You have not (YET) declared how many are in inside you name that array) Other Rules: Arrays can be declared anywhere you can declare a variable Do not use ‘reserved’ words or already used names 8

How to Create an array in two steps (Step 2) Reserve memory for all

How to Create an array in two steps (Step 2) Reserve memory for all of the elements in the list: Array name Keyword array. Of. Ints = new Type int Size semicolon [42] ; You are reserving memory for: The array named array. Of. Ints Needs storage for [42] elements the size of type int Now you (and the compiler) know how many are in inside You cannot change the size after you declare it! Array Of Ints [0] int [1] [2] [3] [4] … [41] int int int 9

How to declare and create an array in one step: Declare and Create at

How to declare and create an array in one step: Declare and Create at the same time: Type Braces Array name int [] Keyword Type array. Of. Ints = new Size int semi [42] ; You are ‘declaring’ that There is an array named array. Of. Ints The 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! 10

Exercise 9 -3: Write the declarations for: Both Steps 1 and 2 on the

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

Exercise: What’s wrong with these? May be valid, maybe not! 12

Hand Initializing an Array works best for a very , very short list int

Hand Initializing an Array works best for a very , very short list int [] stuff = new int[3]; stuff[0] = 8; stuff[1] = 3; stuff[2] = 1; or int [] stuff = { 8, 3, 1 }; 13

Initializing arrays see web example Declare and initialize by hand int[] die = new

Initializing arrays see web example Declare and initialize by hand int[] 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 int n = 0; while (n < 5) { die[n] = random(0, 6); n = n + 1; } 14

Initializing with for loop Declare and initialize by hand int[] die = new int[5];

Initializing with for loop Declare and initialize by hand int[] 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); } Learning Processing: Slides by Don Smith 15

Walking off the end of the array… A very common problem is trying to

Walking off the end of the array… A very common problem is trying to access an element in the array that doesn’t exist: int[] xpos = new int[10]; Remember that we start at element 0 What is the index of the last element? ______ What happens if we try to access element [10]? for (int i = 0; i <= 10; i++ ) { xpos[i] = 0; } Try it and see! Exception in thread "Animation Thread" java. lang. Array. Index. Out. Of. Bounds. Exception: 10 at Out. Of. Bounds. setup(Out. Of. Bounds. java: 21) 16

Additional Processing Array features Processing provides a set of functions to help manage arrays

Additional Processing Array features Processing provides a set of functions to help manage arrays shorten() – shortens array by 1 concat() puts two arrays together subset() takes smaller sections of one array and places them in another append()expands an array by one element splice() inserts elements into another array expand() – Make an array larger in size! default doubles size sort() – Sort all the elements in the array reverse() – Reverse the order of all elements 17

Arrays of Objects Using arrays to store multiple objects is a very powerful programming

Arrays of Objects Using arrays to store multiple objects is a very powerful programming feature we 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

Summary Arrays make it easier to store ‘groups’ or ‘lists’ 1) Must be exactly the same type inside 2) 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

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. width 2. height 3. x location 4. Y location 5. color 6. add any ornamental element you like in addition Interactivity: 1. the button needs to change color when rolled over 2. the buttons need to index through the image array Due next class 20