Can we solve this problem?

Slides:



Advertisements
Similar presentations
1 Various Methods of Populating Arrays Randomly generated integers.
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.
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 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
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.
CSc 110, Autumn 2016 Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges.
Lecture 25: Searching and Sorting
Two-Dimensional Arrays
CSCI 162 – Introduction to Programming II William Killian
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
this keyword this : A reference to the implicit parameter Syntax:
Building Java Programs Chapter 7
this keyword this : A reference to the implicit parameter Syntax:
Building Java Programs
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
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
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 9: Arrays Building Java Programs: A Back to Basics Approach
Lecture 10: Arrays AP Computer Science Principles
CSE 143 Lecture 4 More ArrayIntList:
slides created by Marty Stepp
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
Why did the programmer quit his job?
Array basics Readings: 7.1.
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
Building Java Programs
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.
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
Building Java Programs
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Suggested self-checks: Section 7.11 #12-29
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Implementing ArrayIntList
Presentation transcript:

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. index 1 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 element 4 element 9

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

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?

The Arrays class Class Arrays in package java.util has useful static methods for manipulating arrays: Syntax: Arrays.methodName(parameters) Method name Description binarySearch(array, value) returns the index of the given value in a sorted array (or < 0 if not found) copyOf(array, length) returns a new copy of an array equals(array1, array2) returns true if the two arrays contain same elements in the same order fill(array, value) sets every element to the given value sort(array) arranges the elements into sorted order toString(array) returns a string representing the array, such as "[10, 30, -25, 17]"

"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