1 / 20

Arrays

Arrays. animation scripting. c sc 233. 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. What is an Array.

leone
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays animation scripting csc 233

  2. 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

  3. 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).

  4. 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.

  5. 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

  6. 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

  7. 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!

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

  9. 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 semicolon arrayOfInts = new int [42] ; You are reserving memory for: The array named arrayOfInts 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] [1] [2] [3] [4] … [41] int int int int int int

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

  11. Exercise 9-3: Write the declarations for: • Both Steps 1 and 2 on the same line:

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

  13. Hand Initializing an Arrayworks 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 };

  14. Initializing arrayssee 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; • }

  15. 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

  16. 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 0 What is the index of the last element? ______ What happens if we try to access element [10]? Try it and see! • int[] xpos = new int[10]; • 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)

  17. 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

  18. 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

  19. 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]

  20. 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

More Related