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.

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-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
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:
1 Reference semantics. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[] args) { int a = 7; int b.
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.
Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.6, 4.3.
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.
1 Traversal algorithms. 2 Array traversal traversal: An examination of each element of an array. Traversal algorithms often takes the following form:
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.
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.
int [] scores = new int [10];
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays days until the AP Computer Science test.
Topic 23 arrays - part 3 (tallying, text processing) Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
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.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Computer Programming BCT 1113
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
Building Java Programs
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
CSE 143 Lecture 1 Review: Arrays and objects
Building Java Programs
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Lecture 7-3: Arrays for Tallying; Text Processing
Topic 23 arrays - part 3 (tallying, text processing)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Can we solve this problem?
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
int [] scores = new int [10];
Building Java Programs
Building Java Programs
Java for Beginners University Greenwich Computing At School DASCO
Array basics Readings: 7.1.
Suggested self-checks: Section 7.11 #1-11
Can we solve this problem?
Building Java Programs
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
Building Java Programs
Building Java Programs
File output; Arrays reading: 6.4 – 6.5, 7.1
Building Java Programs
Building Java Programs
Can we solve this problem?
Building Java Programs
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
Arrays.
Implementing ArrayIntList
Week 7 - Monday CS 121.
Presentation transcript:

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 weather data, store each month's weather stats as an element in a large array, and then examine it later to find overall weather statistics.

Array initialization statement Quick array initialization, general syntax: <type> [] <name> = {<value>, <value>, ..., <value>}; Example: int[] numbers = {12, 49, -2, 26, 5, 17, -6}; This syntax is useful when you know in advance what the array's elements will be. You don't explicitly specify the array's size when you use this syntax -- the Java compiler figures this out by looking at the number of values written. index 1 2 3 4 5 6 value 12 49 -2 26 17 -6

Array practice problem What are the contents of the array after the following code? int[] a = {2, 5, 1, 6, 14, 7, 9}; for (int i = 1; i < a.length; i++) { a[i] += a[i - 1]; } index 1 2 3 4 5 6 value 7 8 14 28 35 44 index 1 2 3 4 5 6 value

The Arrays class The Arrays class in package java.util has several useful static methods for manipulating arrays: Method name Description binarySearch(array, value) returns the index of the given value in this array (-1 if not found) equals(array1, array2) whether the two given arrays contain exactly the same elements in the same order fill(array, value) sets every element in the array to have the given value sort(array) arranges the elements in the array into ascending order toString(array) returns a String representing the array, such as "[10, 30, 17]"

Arrays.toString The Arrays.toString method is very useful when you want to print an array's elements separated by commas. Arrays.toString accepts an array as a parameter and returns the String representation, which you can then print. Example: int[] a = {2, 5, 1, 6, 14, 7, 9}; for (int i = 1; i < a.length; i++) { a[i] += a[i - 1]; } System.out.println("a is " + Arrays.toString(a)); Output: a is [2, 7, 8, 14, 28, 35, 44]

Traversal algorithms suggested reading: 7.1, 7.2, 4.4

Array traversal traversal: An examination of each element of an array. Traversal algorithms often take the following form: for (int i = 0; i < <array>.length; i++) { do something with <array> [i]; }

Printing array elements Example (print each element of an array on a line): int[] list = {4, 1, 9, 7}; for (int i = 0; i < list.length; i++) { System.out.println(i + ": " + list[i]); } Output: 0: 4 1: 1 2: 9 3: 7 How could we change the code to print the following? 4, 1, 9, 7 (do not use Arrays.toString.)

Examining array elements Example (find largest even integer in an array): int[] list = {4, 1, 2, 7, 6, 3, 2, 4, 0, 9}; int largestEven = 0; for (int i = 0; i < list.length; i++) { if (list[i] % 2 == 0 && list[i] > largestEven) { largestEven = list[i]; } System.out.println("Largest even: " + largestEven); Output: Largest even: 6

String traversal Strings are like arrays of chars; they also use 0-based indexes. We can write algorithms to traverse strings to compute information: // string stores voters' votes // (R)epublican, (D)emocrat, (I)ndependent String votes = "RDRDRRIDRRRDDDDIRRRDRRRDIDIDDRDDRRDRDIDD"; int[] counts = new int[3]; // R -> 0, D -> 1, I -> 2 for (int i = 0; i < votes.length(); i++) { char c = votes.charAt(i); if (c == 'R') { counts[0]++; } else if (c == 'D') { counts[1]++; } else { // c == 'I') counts[2]++; } System.out.println(Arrays.toString(counts)); Output: [17, 18, 5]

Section attendance problem Imagine an input file containing attendance data. Each line represents a section. Each section has 5 students. Every 5 numbers represent 1 week's attendance record. The first of the 5 marks whether the first student attended. The second of the 5 marks whether the second student attended. ... A mark of 0 means the student did not attend, and a mark of 1 means the student did attend. Example (contains 3 sections and a 9-week attendance record): 111111101011111101001110110110110001110010100 111011111010100110101110101010101110101101010 110101011011011011110110101011010111011010101 week1 week2 week3 week4 week5 week6 week7 week8 week9 11111 11010 11111 10100 11101 10110 11000 11100 10100 week2 student1 student2 student3 student4 student5 1 1 0 1 0

Section attendance problem Write a program that reads the preceding section data file and produces output such as the following: Section #1: Sections attended: [9, 6, 7, 4, 3] Student scores: [20, 18, 20, 12, 9] Student grades: [100.0, 90.0, 100.0, 60.0, 45.0] Section #2: Sections attended: [6, 7, 5, 6, 4] Student scores: [18, 20, 15, 18, 12] Student grades: [90.0, 100.0, 75.0, 90.0, 60.0] Section #3: Sections attended: [5, 6, 5, 7, 6] Student scores: [15, 18, 15, 20, 18] Student grades: [75.0, 90.0, 75.0, 100.0, 90.0] Assume the input file exists and is valid.

Array transformations Problems like the preceding one are examples where we use data in one form to compute new data in another form. We sometimes call this transforming the data. Often each transformation is stored into its own array. Many transformation problems require some sort of mapping between the original data and array indexes. Sometimes the mapping is a tally effect (if the input value is the integer i, store it into array index i ) Sometimes the mapping relates to the position of the original data (store each i th value we read into index i ) Sometimes the mapping must be done explicitly (count occurrences of "X" into index 0, count occurrences of "O" into index 1)