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.

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.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
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 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
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:
CS305j Introduction to Computing Arrays Part 2 1 Topic 20 Arrays part 2 "42 million of anything is a lot." -Doug Burger (commenting on the number of transistors.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
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.
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:
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:
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.
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.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
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
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
1 Building Java Programs Chapter 7: Arrays. 2 Chapter outline array basics declaring and initializing an array getting and setting values of elements.
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.
CSCI 162 – Introduction to Programming II William Killian
Building Java Programs
Dr. Kyung Eun Park Summer 2017
Building Java Programs
Building Java Programs
Building Java Programs
Arrays Part 1 Topic 19 - Stan Kelly-Bootle
Array traversals, text processing
CSC 142 Computer Science II
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
Topic 23 arrays - part 3 (tallying, text processing)
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
python.reset() Also moving to a more reasonable room (CSE 403)
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList
Value and reference semantics; Arrays as parameters
Array basics Readings: 7.1.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Building Java Programs
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.
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList reading:
Implementing ArrayIntList
Presentation transcript:

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 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[-1]); // exception System.out.println(data[9]); // okay System.out.println(data[10]); // exception 2 index value

3 THE ARRAYS CLASS The Arrays class in package java.util has several useful static methods for manipulating arrays: 3 Method nameDescription binarySearch( array, value ) returns the index of the given value in this array (< 0 if not found) equals( array1, array2 ) returns true if 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]"

4 ARRAYS.TOSTRING The Arrays.toString method is useful when you want to print an array's elements.  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] 4

5 ARRAY TRAVERSAL traversal: An examination of each element of an array.  Traversal algorithms often take the following form: for (int i = 0; i.length; i++) { do something with [i]; } Traversals are useful in many situations:  printing the elements of an array  searching an array for a specific value  rearranging the elements of an array  computing the sum, product, etc. of an array's elements ... 5

6 PRINTING ARRAY ELEMENTS Example (print each element of an array on a line): int[] myarray = {4, 1, 9, 7}; for (int i = 0; i < myarray.length; i++) { System.out.println(i + ": " + myarray[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.) 6

7 EXAMINING ARRAY ELEMENTS Example (find the 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 7

8 STRING TRAVERSAL String s are like arrays of char s; 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] 8

9 OUTPUT PARAMETERS output parameter: An array or object passed as a parameter that has its contents altered by the method.  We can pass in an array to a method and the method can change its contents in useful ways for us.  Examples: The methods Arrays.fill and Arrays.sort. int[] nums = {4, -1, 2, 7, 3, 6}; System.out.println(Arrays.toString(nums)); Arrays.sort(nums); // modifies contents of nums System.out.println(Arrays.toString(nums)); Arrays.fill(nums, 42); System.out.println(Arrays.toString(nums)); Output: [4, -1, 2, 7, 3, 6] [-1, 2, 3, 4, 6, 7] [42, 42, 42, 42, 42, 42] 9

10 ARRAYS AS RETURN VALUES An array can also be returned from a method.  Syntax (declaration): public static [] ( ) {  Example: public static int[] readAllNumbers(Scanner input) {  Syntax (call): [] = ( );  Example: Scanner fileScan = new Scanner(new File("temp.dat")); int[] numbers = readAllNumbers(fileScan); 10

11 ARRAY RETURN EXAMPLE Example (digit-counting problem from an earlier slide): public static int[] countDigits(int n) { int[] counts = new int[10]; while (n > 0) { int digit = n % 10; n = n / 10; counts[digit]++; } return counts; } public static void main(String[] args) { int number = ; int[] tally = countDigits(number); System.out.println(Arrays.toString(tally)); } Output: [2, 1, 3, 1, 0, 0, 0, 1, 0, 1] 11

12 STRING METHODS WITH ARRAYS These String methods return arrays: String s = "long book"; 12 Method nameDescriptionExample toCharArray() separates this string into an array of its characters s.toCharArray() returns {'l', 'o', 'n', 'g', ' ', 'b', 'o', 'o', 'k'} split( delimiter ) separates this string into substrings by the given delimiting string s.split(" ") returns {"long", "book"} s.split("o") returns {"l", "ng b", "", "k"}

13 STRING/ARRAY PROBLEMS Write a method named areAnagrams that accepts two String s as parameters and returns whether those strings contain the same letters (in any order).  areAnagrams("bear", "bare") returns true  areAnagrams("sale", "sail") returns false  Use the methods from the previous slide, as well as any relevant methods from the Arrays class. Write a method named wordCount that accepts a String as its parameter and returns the number of words in that string. Words are separated by spaces.  wordCount("the quick brown fox") returns 4  Use the methods from the previous slide. 13

SHIFTING ELEMENTS IN AN ARRAY reading:

15 CONCEPT OF AN ARRAY ROTATION Imagine we want to 'rotate' the elements of an array; that is, to shift them left by one index.  The element that used to be at index 0 will move to the last slot in the array.  For example, {3, 8, 9, 7, 5} becomes {8, 9, 7, 5, 3}. Before: After:  Shifting elements is useful when inserting and removing values from arrays after they have already been filled with data. 15 index01234 value38975 index01234 value89753

16 SHIFTING ELEMENTS LEFT A left shift of the elements of an array: Let's write the code to do the left shift.  Can we generalize it so that it will work on an array of any size?  Can we write a right-shift as well? 16 index01234 value38975 index01234 value89753

17 SHIFTING PRACTICE PROBLEM Write a method insertInOrder that accepts a sorted array a of integers and an integer value n as parameters, and inserts n into a while maintaining sorted order. In other words, assume that the element values in a occur in sorted ascending order, and insert the new value n into the array at the appropriate index, shifting to make room if necessary. The last element in the array will be lost after the insertion.  Example: calling insertInOrder on array {1, 3, 7, 10, 12, 15, 22, 47, 74} and value 11 produces {1, 3, 7, 10, 11, 12, 15, 22, 47}. 17