University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar 8 2010

Slides:



Advertisements
Similar presentations
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
Advertisements

CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Arrays II Lecture 15, Thu Mar
Programming with Collections Collections in Java Using Arrays Week 9.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design Lecture 6, Tue Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Objects, Input Lecture 7, Wed Jan
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 2D Arrays, Sorting Lecture 16, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
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.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays II Lecture 22, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 30, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Interfaces, Polymorphism Lecture 29, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 2D Arrays, Sorting Lecture 23, Fri Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Arrays and Class Design Lecture 18, Tue Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 12, Mon Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Yet More Array Practice Lecture 24, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 13, Wed Feb
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Hello AP Computer Science!. What are some of the things that you have used computers for?
CS1101: Programming Methodology Aaron Tan.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
1 CS 007: Introduction to Computer Programming Ihsan Ayyub Qazi.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Arrays.
Introduction to Computing Concepts Note Set 21. Arrays Declaring Initializing Accessing Using with Loops Sending to methods.
BIT115: Introduction to Programming
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 6_1 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Class Design II Lecture 7, Thu Jan
BIT115: Introduction to Programming
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar borrowing from slides by Kurt Eiselt

2 Reading n This week: n Chap 7: 7.1, Topics 7.3 and 7.4 (3rd ed) n Chap 8: 8.1, Topics 6.3 and 6.4 (2nd ed)

3 News n Midterms returned before break n get yours after class if you didn't already n Departmental announcements

4 Department of Computer Science Undergraduate Events Events this week Drop-In Resume and Cover Letter Editing (20 min. appointments) Date: Thurs., March 11 Time: 11:30 am – 2:30 pm Location: Rm 255, ICICS/CS Townhall Meeting for Combined Majors /Honours, Cogs, BA, B.Comm in CS Date: Thurs., March 11 Time: 12:30 – 2 pm Location: DMP 310 Lumch will be provided! CS Distinguished Lecture Series Featuring David Parkes Title: Incentive Mechanism Engineering in the Internet Age Date: Thurs., Mar 11 Time: 3:30 – 4:50 pm Location: DMP 110 CSSS Movie Night: “Zombieland” & “Iron Man” Date: Thurs., Mar 11 Time: 6 – 10 pm Location: DMP 310 Free pop & popcorn for every attendee! Events next week Interview Skills Practice Session Mon., March 15, 12 – 2 pm, Rm 202, ICICS/CS Transport Canada Info Session Tues., March 16, 4 – 6 pm, HENN 201 Financial Literacy 101 Wed., March 17, 12 – 1 pm, Angus 426 Townhall Meeting for CS Major/Honours Students Thurs., March 18, 12:30 – 2 pm, DMP 310 CS Distinguished Lecture Series Featuring Jeff Hawkins Thurs., March 18, 3:30 – 4:50 pm, DMP 110

5 Cans of pop sold this month What’s the gross income? What’s the net profit? Is Bubba stealing loonies? Keeping Track of Things

6 Cans of pop sold this month In other words, how can I organize the data above in my computer so that I can access it easily and do the computations I need to do? Keeping Track of Things

7 Cans of pop sold this month Answer: Arrays n use arrays: common programming language construct n grouping related data items together n meaningful organization such that each individual data item can be easily retrieved or updated

8 Answer: Arrays n use arrays: common programming language construct n grouping related data items together n meaningful organization such that each individual data item can be easily retrieved or updated n collection of variables n all of same type n share common name n each variable holds single value cansSold

9 cansSold Using Arrays n Collection of variables has single name n how do we access individual values?

10 cansSold Using Arrays n Collection of variables has single name n how do we access individual values? n Each value stored at unique numbered position n number called index of array element n aka subscript n cansSold name of this array n holds 10 values

11 cansSold Using Arrays n To access individual value in array n use array name followed by pair of square brackets n inside brackets, place index of array element we want to access n Reference to array element allowed anywhere that variables can be used n Example: System.out.println(cansSold[4]); n Prints value 209

12 cansSold Array Declaration and Types n Just like ordinary variable, must n declare array before we use it n give array a type n Since cansSold contains integers, make integer array: int[] cansSold = new int[10] n Looks like variable declaration, except:

13 cansSold Array Declaration and Types n Just like ordinary variable, must n declare array before we use it n give array a type n Since cansSold contains integers, make integer array: int[] cansSold = new int[10] n Looks like variable declaration, except: n empty brackets on the left tell Java that cansSold is an array...

14 cansSold Array Declaration and Types n Just like ordinary variable, must n declare array before we use it n give array a type n Since cansSold contains integers, make integer array: int[] cansSold = new int[10] n Looks like variable declaration, except: n empty brackets on the left tell Java that cansSold is an array... n the number in the brackets on the right tell Java that array should have room for 10 elements when it's created

15 cansSold Array Declaration and Types n Just like ordinary variable, must n declare array before we use it n give array a type n Since cansSold contains integers, make integer array: int[10] cansSold = new int[10] n Looks like variable declaration, except: n empty brackets on the left tell Java that cansSold is an array... n the number in the brackets on the right tell Java that array should have room for 10 elements when it's created n DO NOT put size of array in brackets on the left

16 cansSold Array Declaration and Types n Just like ordinary variable, must n declare array before we use it n give array a type n Since cansSold contains integers, make integer array: int[10] cansSold = new int[10] n Looks like variable declaration, except: n empty brackets on the left tell Java that cansSold is an array... n the number in the brackets on the right tell Java that array should have room for 10 elements when it's created n DO NOT put size of array in brackets on the left

17 cansSold Array Declaration and Types public class ArrayTest1 { public static void main(String[] args) { final int ARRAYSIZE = 10; int[] cansSold = new int[ARRAYSIZE]; cansSold[0] = 185; cansSold[1] = 92; cansSold[2] = 370; cansSold[3] = 485; cansSold[4] = 209; cansSold[5] = 128; cansSold[6] = 84; cansSold[7] = 151; cansSold[8] = 32; cansSold[9] = 563; // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); }

18 cansSold Array Declaration and Types public class ArrayTest2 { public static void main(String[] args) { int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); } n Can also use initializer list n Right side of declaration does not include type or size n Java figures out size by itself n Types of values on right must match type declared on left n Initializer list may only be used when array is first declared

19 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result

20 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { }

21 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { }

22 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; }

23 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; }

24 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; }

25 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; }

26 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) }

27 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } }

28 cansSold Using Arrays and Loops n Write program to n create array n find total number of cans sold n print result public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }

29 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }

30 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } totalCans 0

31 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold totalCans 0 cansSold.length 10

32 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 0 cansSold.length 10 0

33 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n Is i < 10? n yes, 0 < 10 cansSold i totalCans 0 cansSold.length 10 0

34 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 185 cansSold.length 10 0

35 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 185 cansSold.length 10 1

36 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n Is i < 10? n yes, 1 < 10 cansSold i totalCans 185 cansSold.length 10 1

37 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 277 cansSold.length 10 1

38 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 277 cansSold.length 10 2

39 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n Is i < 10? n yes, 2 < 10 cansSold i totalCans 277 cansSold.length 10 2

40 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 647 cansSold.length 10 2

41 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 647 cansSold.length 10 3

42 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n Is i < 10? n yes, 3 < 10 cansSold i totalCans 647 cansSold.length 10 3

43 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 1132 cansSold.length 10 3

44 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 1132 cansSold.length 10 4

45 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n Is i < 10? n yes, 4 < 10 cansSold i totalCans 1132 cansSold.length 10 4

46 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n And so on… cansSold i totalCans 1341 cansSold.length 10 4

47 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n And so on… cansSold i totalCans 1469 cansSold.length 10 5

48 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n And so on… cansSold i totalCans 1553 cansSold.length 10 6

49 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n And so on… cansSold i totalCans 1704 cansSold.length 10 7

50 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n And so on… cansSold i totalCans 1736 cansSold.length 10 8

51 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n And so on… cansSold i totalCans 2299 cansSold.length 10 9

52 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } cansSold i totalCans 2299 cansSold.length 10

53 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n Is i < 10? n no, 10 not < 10 cansSold i totalCans 2299 cansSold.length 10

54 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n "We've sold 2299 cans of pop" printed out cansSold i totalCans 2299 cansSold.length 10

55 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i <= cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n What would happen if we made this little change? cansSold i totalCans 2299 cansSold.length 10

56 Tracing Arrays and Loops public class ArrayTest3 { public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i <= cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); } n What would happen if we made this little change? java.lang.ArrayIndexOutOfBoundsException: 10 cansSold i totalCans 2299 cansSold.length 10

57 cansSold cansSold.length Something To Remember n Array cansSold created with 10 elements n Indices (plural of index) are 0 through 9 n In general, array of size n will have indices ranging from 0 through n-1 n When you number things, you're used to beginning with 1 n Computer folks begin with 0 n leads to "off by one" errors, even among computer veterans

58 Initializing Array With Keyboard Input import java.util.Scanner; public class ArrayTest3b { public static void main(String[] args) { final int ARRAYSIZE = 10; int[] cansSold = new int[ARRAYSIZE]; Scanner scan = new Scanner(System.in); for (int i = 0; i < cansSold.length; i++) { System.out.print("Enter machine " + (i+1)); cansSold[i] = scan.nextInt(); } // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); } cansSold

numbers Averaging Loop Example n Let's say we want to write a program that prints average of values in some arbitrarily large array n like the one to the left called numbers n Will require loop n Simple task for looping in the context of an array n how will we make this happen?

numbers PrintMax Loop Example n Now instead of average, we want to find and print maximum value from some arbitrarily large array n Similar loop, but with some extra tweaks.

61 Histogram Loop Example n Now use same data as basis for histogram n Write one loop to look at value associated with each row of array n for each value print a line with that many asterisks n For example, if program reads value 6 from the array, should print line of 6 asterisks n Program then reads the value 8, prints a line of 8 asterisks, and so on. n Need outer loop to read individual values in the array n Need inner loop to print asterisks for each value ****** 8 ******** 11 *********** 18 ****************** 20 ******************** 17 ***************** 14 ************** 10 ********** 5 ***** 2 ** numbers

62 Storing Different Data Types cansSold

63 Storing Different Data Types cansSold cashIn Could use two arrays of same size but with different types

64 cansSold cashIn Could use two arrays of same size but with different types Storing Different Data Types n Write program to compare what's been collected from each machine vs. how much should have been collected?

65 cansSold cashIn Could use two arrays of same size but with different types Storing Different Data Types n Write program to compare what's been collected from each machine vs. how much should have been collected? public class ArrayTest4 { public static void main(String[] args) { double expected; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; double[] cashIn = {201.25, , , , , , , , 40.00, }; for (int i = 0; i < cansSold.length; i++) { expected = cansSold[i] * 1.25; System.out.println("Machine " + (i + 1) + " off by $" + (expected - cashIn[i])); }

66 cansSold cashIn Could use two arrays of same size but with different types What happens when we run the program? Storing Different Data Types n Write program to compare what's been collected from each machine vs. how much should have been collected? public class ArrayTest4 { public static void main(String[] args) { double expected; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; double[] cashIn = {201.25, , , , , , , , 40.00, }; for (int i = 0; i < cansSold.length; i++) { expected = cansSold[i] * 1.25; System.out.println("Machine " + (i + 1) + " off by $" + (expected - cashIn[i])); }

67 cansSold Machine 0 off by $30.0 Machine 1 off by $14.5 Machine 2 off by $49.75 Machine 3 off by $51.0 Machine 4 off by $66.25 Machine 5 off by $0.0 Machine 6 off by $0.0 Machine 7 off by $0.0 Machine 8 off by $0.0 Machine 9 off by $0.0 cashIn Somebody has been stealing from the machines after all! We need an anti-theft plan… Storing Different Data Types

68 cansSold cashIn Arrays With Non-Primitive Types n Great if you're always storing primitives like integers or floating point numbers n What if we want to store String types too? n remember that String is an object, not a primitive data type

69 cansSold cashIn Arrays With Non-Primitive Types n Then we create array of objects n In this case objects will be Strings n Array won't hold actual object n holds references: pointers to objects String[] location = new String[10]; location

70 cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location "Chan Centre"

71 cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location[1] = "Law School"; location "Chan Centre" "Law School"

72 cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location[1] = "Law School"; location[2] = "Main Library"; location "Chan Centre" "Law School" "Main Library"

73 cansSold cashIn Arrays of Objects n Now we can put references to Strings in our String array. location[0] = "Chan Centre"; location[1] = "Law School"; location[2] = "Main Library";...and so on... location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

74 cansSold cashIn Arrays of Objects n Or we could have done this: String[] location = {"Chan Centre", "Law School", "Main Library",.... }; location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

75 cansSold cashIn Arrays of Objects n Each individual String object in array of course has all String methods available n For example, what would this return? location[2].length() location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

76 cansSold cashIn Arrays of Objects n Each individual String object in array of course has all String methods available n For example, what would this return? location[2].length() n 12 location "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science"

Arrays of Objects n Think about a cleaner way to do all this… "Chan Centre" "Law School" "Main Library" "Koerner Library" "Business" "Biology" "Education" "Applied Science" "Agriculture" "Computer Science" location cashIn cansSold