 9: Arrays  Why?  What is an Array?  Declaring and Creating an Array  Initializing and Array  Array Operations  Array Examples  Arrays of Objects.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
An introduction to pointers in c
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Lesson Four: More of the Same
Chapter 10 Introduction to Arrays
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Lecture 7: Arrays Yoni Fridman 7/9/01 7/9/01. OutlineOutline ä Back to last lecture – using the debugger ä What are arrays? ä Creating arrays ä Using.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Chapter 9 Introduction to Arrays
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Lec 19 Array Intro. Agenda Arrays—what are they Declaring Arrays Constructing Arrays Accessing Array cells.
Lists in Python.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
CIT 590 Intro to Programming First lecture on Java.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
Arrays Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Linked List by Chapter 5 Linked List by
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
Programming Games Logic. Slide show. Range input. Storage. Datatypes. Binary numbers. Homework: Catch up. This includes uploading projects to your server.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Review Array – int[] diameters = new int[10]; – diameters[0], diameters[2], diameters[9] – diameters.length Indexing starts at 0 A way to have a collection.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Review Objects – data fields – constructors – Methods Classes.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
Arrays. 2 Why do we care (about arrays)? What if you have a whole bunch of cars (or aliens or balls or ???) bouncing around the screen? How do we keep.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
4. Java language basics: Function
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 5 - Functions Outline 5.1 Introduction
Java Programming Language
Presentation transcript:

 9: Arrays  Why?  What is an Array?  Declaring and Creating an Array  Initializing and Array  Array Operations  Array Examples  Arrays of Objects 2

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

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

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

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

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

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 datatype int 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 8

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! 9 Array Of Ints [0] [1] [2] [3] [4] … [41] int

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

 Both Steps 1 and 2 on the same line: 11

 May be valid, maybe not! 12

13 int [] stuff = new int[3]; stuff[0] = 8; stuff[1] = 3; stuff[2] = 1; or int [] stuff = { 8, 3, 1 };

14 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; }

Learning Processing: Slides by Don Smith15 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); } VS. Using a while loop for (int n = 0; n < 5; n++ ) { die[n] = (int)random(0,6); }

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! 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];

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

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 Smith18

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

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