Lecture 9: Arrays Building Java Programs: A Back to Basics Approach

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Building Java Programs Chapter 7
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
BUILDING JAVA PROGRAMS CHAPTER 7: ARRAYS 1. 2 OUT-OF-BOUNDS INDEXES The indexes that are legal to access in an array are those in the range of 0 to the.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
Lecture 8: Return values and math
Lecture 6: While Loops and the Math Class
CSCI 162 – Introduction to Programming II William Killian
[ 4.00 ] [ Today’s Date ] [ Instructor Name ]
Building Java Programs
Lecture 13: More Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
CSC 142 Computer Science II
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
Lecture 7: Graphics, return values and math
Building Java Programs
CSE 143 Lecture 1 Review: Arrays and objects
Building Java Programs
Building Java Programs
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Lecture 22: lists Adapted from slides by Marty Stepp and Stuart Reges
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs Chapter 7
Can we solve this problem?
python.reset() Also moving to a more reasonable room (CSE 403)
Lecture 19: lists Adapted from slides by Marty Stepp and Stuart Reges
Lecture 13: Two-Dimensional Arrays
Lecture 10: Arrays AP Computer Science Principles
Lecture 9:The While Loop + Math methods AP Computer Science Principles
Lecture 12: 2D Arrays AP Computer Science Principles
Lecture 10: Arrays II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Array basics Readings: 7.1.
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Can we solve this problem?
Building Java Programs
Presentation transcript:

Lecture 9: Arrays Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson 2013. All rights reserved.

Can we solve this problem? Consider the following program (input underlined): How many days' temperatures? 7 Day 1's high temp: 45 Day 2's high temp: 44 Day 3's high temp: 39 Day 4's high temp: 48 Day 5's high temp: 37 Day 6's high temp: 46 Day 7's high temp: 53 Average temp = 44.6 4 days were above average.

Arrays array: object that stores many values of the same type. element: One value in an array. index: A 0-based integer to access an element from an array. index 1 2 3 4 5 6 7 8 9 value 12 49 -2 26 17 -6 84 72 element 0 value is 12 element 4 value is 5 element 9 value is 3

Array declaration type[] name = new type[length]; Example: int[] numbers = new int[10]; index 1 2 3 4 5 6 7 8 9 value

Array declaration, cont. The length can be any integer expression. int x = 2 * 3 + 1; int[] data = new int[x % 5 + 2]; Each element initially gets a "zero-equivalent" value. Type Default value int double 0.0 boolean false String or other object null (means, "no object")

Accessing elements name[index] // access name[index] = value; // modify Example: numbers[0] = 27; numbers[3] = -6; System.out.println(numbers[0]); if (numbers[3] < 0) { System.out.println("Element 3 is negative."); } index 1 2 3 4 5 6 7 8 9 value 27 -6 index 1 2 3 4 5 6 7 8 9 value

Arrays of other types double[] results = new double[5]; results[2] = 3.4; results[4] = -0.5; boolean[] tests = new boolean[6]; tests[3] = true; index 1 2 3 4 value 0.0 3.4 -0.5 index 1 2 3 4 5 value false true

Out-of-bounds Legal indexes: between 0 and the array's length - 1. Reading or writing any index outside this range will throw an ArrayIndexOutOfBoundsException. Example: int[] data = new int[10]; System.out.println(data[0]); // okay System.out.println(data[9]); // okay System.out.println(data[-1]); // exception System.out.println(data[10]); // exception index 1 2 3 4 5 6 7 8 9 value

Accessing array elements int[] numbers = new int[8]; numbers[1] = 3; numbers[4] = 99; numbers[6] = 2; int x = numbers[1]; numbers[x] = 42; numbers[numbers[6]] = 11; // use numbers[6] as index x 3 x index 1 2 3 4 5 6 7 value index 1 2 3 4 5 6 7 value 11 42 99 numbers

Arrays and for loops It is common to use for loops to access array elements. for (int i = 0; i < 8; i++) { System.out.print(numbers[i] + " "); } System.out.println(); // output: 0 3 11 42 99 0 2 0 Sometimes we assign each element a value in a loop. numbers[i] = 2 * i; index 1 2 3 4 5 6 7 value 8 10 12 14

The length field An array's length field stores its number of elements. name.length for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } // output: 0 2 4 6 8 10 12 14 It does not use parentheses like a String's .length(). What expressions refer to: The last element of any array? The middle element?

Quick array initialization type[] name = {value, value, … value}; Example: int[] numbers = {12, 49, -2, 26, 5, 17, -6}; Useful when you know what the array's elements will be The compiler figures out the size by counting the values index 1 2 3 4 5 6 value 12 49 -2 26 17 -6

"Array mystery" problem traversal: An examination of each element of an array. What element values are stored in the following array? int[] a = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; }

"Array mystery" problem traversal: An examination of each element of an array. What element values are stored in the following array? int[] a = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } index 1 2 3 4 5 6 value 7 10 12 8 14 22 index 1 2 3 4 5 6 value

Limitations of arrays You cannot resize an existing array: int[] a = new int[4]; a.length = 10; // error You cannot compare arrays with == or equals: int[] a1 = {42, -7, 1, 15}; int[] a2 = {42, -7, 1, 15}; if (a1 == a2) { ... } // false! if (a1.equals(a2)) { ... } // false! An array does not know how to print itself: System.out.println(a1); // [I@98f8c4]

Arrays.toString public static void main(String[] args) { int[] a = {0, 14, 4, 6, 8}; System.out.println(a); } Output: I@674f1c67 6. (14 pts) Prints out the address not the contents of a. Arrays.toString accepts an array as a parameter and returns a String representation of its elements. Must import java.util.*; Arrays is one of the classes in the util’s package.

Arrays.toString import java.util.*; public class Example public static void main(String[] args) { int[] a = {0, 14, 4, 6, 8}; System.out.println(”a is " + Arrays.toString(a)); } Output: a is [0, 14, 4, 6, 8]

float vs double float: 32-bit data type representing real numbers to about 7 decimal places. double: 64-bit data type representing real numbers to about 16 decimal places. Since Processing is graphics intensive, it is recommended that floats are used instead of doubles. In fact, float is the default type for decimal numbers in Processing. All of the built-in math functions in Processing returns floats. The math functions from the standard Math library in Java returns doubles. double a = Math.sqrt(9); // a = 3.0 but is a double float b = sqrt(9); // b = 3.0 but is a float

float vs double

Processing's Math methods Method name Description abs(value) absolute value(int or float) ceil(value) rounds up(int) exp(value) exponential, base e log(value) logarithm, base e map(x,x1,x2,y1,y2) linear map x from [x1,x2] to [y1,y2] pow(base, exp) base to the exp power(float) random(value) random float from [0,value) random(value1, value2) random float from [value1,value2) round(value) nearest whole number(int) sqrt(value) square root sin(value),cos(value) tan(value) sine/cosine/tangent of an angle in radians atan(value) atan2(dy,dx) inverse tangent (-PI/2, PI/2) inverse tangent (-PI, PI) degrees(value) radians(value) convert degrees to radians and back Constant Description PI 3.1415926...

Arrays String[] a=[“hip”, “hip”]; //hip hip arrays!

Arrays He didn’t get arrays and he didn’t get a raise.

Array Lab 1 Write the method average which accepts an int array and returns the average of the values. Write the method countAboveAve which accepts an int array and returns the number of values that are above the average. You must call average. Write two methods largest and indexOfsmallest which accept an int array and returns the largest value and the index of the smallest value, respectively. If there are multiple smallest values, return the index of the first one. Also write the main method with an array and check to make sure your methods work! public static double average(int[] array){} public static int countAboveAve(int[] array){} public static int largest(int[] array){} public static int indexOfsmallest(int[] array){}

Array Lab 2 Write a Processing program generate a large number of balls with random starting position that moves left and right at random speeds. You’ll need 4 arrays: an array for x positions, an array for y positions, an array for its xspeed and one more for its diameter(or radius). The setup() method randomizes position, speed and diameters with a for loop. The draw() method draw and moves and bounces the balls.

Array Lab 2 Add the method indexLargestDiameter which returns the index of the largest ball. Draw this largest ball in blue. It is recommended that you try to do this on your own but there is a .pde template file on my github website if you need some help.