1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, 2006. They may not be rehosted, sold, or.

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.
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  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Building Java Programs Chapter 7 Arrays. 2 Can we solve this problem? Consider the following program (input underlined): How many days' temperatures?
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CS 112 Introduction to Programming Arrays; Loop Patterns (break) Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 Building Java Programs Chapter 2: Primitive Data and Definite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3.
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.
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.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
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.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
CSC 142 Computer Science II
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Building Java Programs
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Lecture 15: lists Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
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
Building Java Programs
Array basics Readings: 7.1.
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
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:

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 modified without expressed permission from the authors. All rights reserved.

2 Chapter outline Array basics declaring and initializing an array getting and setting values of elements of an array limitations of arrays Arrays for counting and tallying Array traversal algorithms Advanced array usage Shifting elements in an array arrays as parameters to methods String methods that use arrays Graphics methods that use arrays the Arrays class command-line arguments

3 Array basics suggested reading:7.1

4 A problem we can't solve (yet) 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 = days were above average. We need access to the temperatures once to compute the average, and then again to tell how many were above average.

5 Why the problem is tough We appear to need each input value twice: once to compute the average a second time to count how many were above average We could examine each value twice if we could read them into variables. However, we don't know in advance how many variables we'll need, because we don't know how many days' weather are coming until the program is running. We need a way to declare many variables' worth of memory in a single step.

6 Another tough problem Given a file of integer exam scores, such as: Write a program that will print a text histogram of stars indicating the number of students who earned each unique exam score. 85: ***** 86: ************ 87: *** 88: * 91: ****

7 Arrays array: A single variable that can store many values of the same type. element: One value in an array. index: A 0-based integer, which is used to access an element from an array. We usually draw an array as a row or column of boxes. Example: an array of ten integers index value element 0element 4element 9

8 Array declaration Declaring/initializing an array: [] = new [ ]; Example: int[] numbers = new int[10]; The length of the array is specified between [ ] brackets. The array's length can be any expression, such as a constant or variable's value. Example: int x = 2 * 3 + 1; int[] numbers = new int[x % 5 + 2]; index value

9 Array auto-initialization When arrays are initially constructed, every element is automatically initialized to a "zero-equivalent" value. int : 0 double : 0.0 boolean : false char : '\0' (the "null character") String or object: null ( null means "no object") An array of integers An array of real numbers index01234 value00000 index0123 value0.0

10 Assigning a value to an array element: [ ] = ; Example: numbers[0] = 27; numbers[3] = -6; Using an array element's value in an expression: [ ] Example: System.out.println(numbers[0]); if (numbers[3] < 0) { System.out.println("Element 3 is negative."); } Accessing array elements index value

11 Out-of-bounds indexes The only indexes that are legal to access in an array are those in the range of 0 to the array's length - 1 Reading or writing any index outside this range will crash your program with an ArrayIndexOutOfBoundsException. Example: int[] data = new int[10]; System.out.println(data[0]); // okay System.out.println(data[-1]); // exception System.out.println(data[9]); // okay System.out.println(data[10]); // exception index value

12 Arrays of other types Arrays can contain other types, such as double. Example: double[] results = new double[6]; results[2] = 3.4; results[5] = -0.5; Example: boolean[] tests = new boolean[6]; tests[3] = true; index value index valuefalse truefalse

13 Accessing array elements A longer example of accessing and changing elements: int[] numbers = new int[8]; numbers[1] = 4; numbers[4] = 99; numbers[7] = 2; int x = numbers[1]; numbers[x] = 44; numbers[numbers[7]] = 11; // use numbers[7] as index x4 numbers

14 Arrays and for loops Arrays are very commonly used with for loops that pass over each element and process it in some way: Example (print each element of an array): for (int i = 0; i < 8; i++) { System.out.print(numbers[i] + " "); } System.out.println(); // end the line of output Output (when used on array from previous slide):

15 More arrays and for loops Sometimes we assign each array element a value in a for loop. Example: for (int i = 0; i < 8; i++) { numbers[i] = 2 * i; } What values would be stored into the array after this code? for (int i = 0; i < 8; i++) { numbers[i] = i * i; } Notice that the code in the previous slides refers to the array's length of 8 many times. What's bad about this? index value value

16 An array has a field named.length that returns its total number of elements. General syntax:.length Notice that it doesn't have parentheses like a String's.length(). Example: for (int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + " "); } Output: What expression refers to the last element of the array? The middle element? The.length field

17 Arrays for counting and tallying suggested reading:7.1

18 A multi-counter problem Imagine that we want to examine a large integer and count the number of occurrences of every digit from 0 through 9. Example: the number contains two 0s, one 1, three 2s, one 7, and one 9. We need to examine each digit of the large integer and count how many times we've seen that digit. This will require counters for each of the values We could declare 10 counter variables for this, or (preferred) we could use an array of size 10. The element with index i will store the counter for digit value i.

19 Creating an array of tallies The following code builds an array of digit counters: int num = ; int[] counts = new int[10]; while (num > 0) { int digit = num % 10; counts[digit]++; num = num / 10; } You can watch the array build itself by running the code in the Jeliot Java program visualizer: index value

20 Array histogram question Given a file of integer exam scores, such as: Write a program that will print a histogram of stars indicating the number of students who earned each unique exam score. 85: ***** 86: ************ 87: *** 88: * 91: **** Time permitting, try graphing the data with a DrawingPanel.

21 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 print out the numbers in reverse order. Arrays can help us to group related data into elements. Example: For a given school exam, open a file full of exam scores and count up how many students got each score from 0 through 100. Arrays let us hold on to data and access it in random order. Example: Read a file full of babies' names, store each name's data as an element in a large array, and then examine it later to find many names that the user types.