Programming with Arrays 1

Slides:



Advertisements
Similar presentations
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
ArrayList, Multidimensional Arrays
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Lecture 18/19 Arrays COMP1681 / SE15 Introduction to Programming.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Arrays Chapter 7.
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
CMSC 202 ArrayList Aug 9, 2007.
4. Java language basics: Function
CSC111 Quick Revision.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Arrays in Classes and Methods
Arrays 3/4 By Pius Nyaanga.
Chapter 8 – Arrays and Array Lists
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
SELECTION STATEMENTS (1)
Building Java Programs
CSS161: Fundamentals of Computing
Program Style Console Input and Output
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
CSS161: Fundamentals of Computing
Building Java Programs Chapter 7
Building Java Programs
CMSC 202 ArrayList Aug 9, 2007.
int [] scores = new int [10];
Building Java Programs
Building Java Programs
Defining methods and more arrays
CMSC 202 ArrayList Aug 9, 2007.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
int [] scores = new int [10];
Arrays and Array Lists CS 21a.
Announcements Lab 6 was due today Lab 7 assigned this Friday
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
Dr. Sampath Jayarathna Cal Poly Pomona
CSS161: Fundamentals of Computing
CSS161: Fundamentals of Computing
Building Java Programs
Building Java Programs
Programming with Arrays 2
Building Java Programs
CSS161: Fundamentals of Computing
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
9-10 Classes: A Deeper Look.
Presentation transcript:

Programming with Arrays 1 This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

Partially Filled Arrays The exact size needed for an array may be unknown till run time. Solution: Declare a larger array: someArray[MAX] Keep track of # valid elements in count someArray[0] ~ someArray[count – 1] Where count <= someArray.length CSS161: Fundamentals of Computing

An Array Implementation of a List List is a data structure to maintain a dynamic size of items in a sequential order. 10.0 20.0 30.0 40.0 50.0 60.0 70.0 List.add( ) X 10.0 20.0 30.0 40.0 50.0 60.0 70.0 List.deleteLast( ) X 10.0 20.0 30.0 40.0 50.0 60.0 List.delete( 3 ) 10.0 20.0 30.0 50.0 60.0 List.getElement( 3 ) returns 50.0 CSS161: Fundamentals of Computing

List Implementation (1 of 5) /** List.java: demonstrates a use of a partially filled array of doubles. The class enforces the following invariant: All elements are at the beginning of the array in locations 0, 1, 2, and so forth up to a highest index with no gaps. */ public class List { private int maxNumberElements; //Same as a.length private double[] a; private int numberUsed; //Number of indices currently in use Sets the maximum number of allowable elements to 10. List( ) maxNumberElements = 10; a = new double[maxNumberElements]; numberUsed = 0; } CSS161: Fundamentals of Computing

List Implementation (2 of 5) /** Precondition arraySize > 0. */ List(int arraySize) { if (arraySize <= 0) System.out.println("Error Array size zero or negative."); System.exit(0); } maxNumberElements = arraySize; a = new double[maxNumberElements]; numberUsed = 0; List(List original) if (original == null) System.out.println("Fatal Error: aborting program."); maxNumberElements = original.maxNumberElements; numberUsed = original.numberUsed; for (int i = 0; i < numberUsed; i++) a[i] = original.a[i]; CSS161: Fundamentals of Computing

List Implementation (3 of 5) /** Adds newElement to the first unused array position. */ public void add(double newElement) { if (numberUsed >= a.length) System.out.println("Error: Adding to a full array."); System.exit(0); } else a[numberUsed] = newElement; numberUsed++; public double getElement(int index) if (index < 0 || index >= numberUsed) System.out.println("Error:Illegal or unused index."); return a[index]; CSS161: Fundamentals of Computing

List Implementation (4 of 5) /** index must be an index in use or the first unused index. */ public void resetElement(int index, double newValue) { if (index < 0 || index >= maxNumberElements) System.out.println("Error:Illegal index."); System.exit(0); } else if (index > numberUsed) System.out.println( "Error: Changing an index that is too large."); else a[index] = newValue; public void deleteLast( ) if (empty( )) System.out.println("Error:Deleting from an empty array."); numberUsed--; CSS161: Fundamentals of Computing

List Implementation (5 of 5) /** Deletes the element in position index. Moves down all elements with indices higher than the deleted element. */ public void delete(int index) { if (index < 0 || index >= numberUsed) System.out.println("Error:Illegal or unused index."); System.exit(0); } for (int i = index; i < numberUsed; i++) a[i] = a[i + 1]; // what if numberUsed reached a.length? numberUsed--; public boolean empty( ) return (numberUsed == 0); public boolean full( ) return (numberUsed == maxNumberElements); public int getMaxCapacity( ) { return maxNumberElements; } public int getNumberOfElements( ) return numberUsed; CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing List Driver public class ListDriver { public static void main( String[] args ) { List list = new List( 20 ); for ( int i = 0; i < 20; i++ ) list.add( i ); list.deleteLast( ); // what if this statement is omitted? list.delete( 10 ); for ( int i = 0; i < list.getNumberOfElements( ); i++ ) System.out.println( "list(" + i + ") = " + list.getElement( i ) ); } [mfukuda@perseus css161]$ java ListDriver list(0) = 0.0 list(1) = 1.0 list(2) = 2.0 list(3) = 3.0 list(4) = 4.0 list(5) = 5.0 list(6) = 6.0 list(7) = 7.0 list(8) = 8.0 list(9) = 9.0 list(10) = 11.0 list(11) = 12.0 list(12) = 13.0 list(13) = 14.0 list(14) = 15.0 list(15) = 16.0 list(16) = 17.0 list(17) = 18.0 [mfukuda@perseus css161]$ CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Question What if we delete the following statement from ListDriver.java program? list.deleteLast( ); Keep track of the program execution. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing “for-each” Loop ordinary for loop for-each loop double[] a = new double[10]; for ( int i = 0; i < a.length; i++ ) a[i] = 0.0; System.out.println( a[i] ); a[i] = 2 * i; double[] a = new double[10]; for ( double element : a ) element = 0.0; System.out.println( element ); element = 2 * i; // ??? Was i defined ??? CSS161: Fundamentals of Computing

Vararg Specification Method with a Variable Number of Parameters Syntax Type… Array_Name Examples int… arg double… a String… unwanted public static int max( int… arg ) { // arg can be used as an array of ints } max( score1, score2 ); max( score1, score2, score3 ); score1 score2 score1 score2 score3 CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Display 6.7 (1 of 2) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Display 6.7 (2 of 2) import java.util.Scanner; public class VariableParameterDemo { public static void main(String[] args) System.out.println("Enter scores for Tom, Dick, and Harriet:"); Scanner keyboard = new Scanner(System.in); int tomsScore = keyboard.nextInt( ); int dicksScore = keyboard.nextInt( ); int harrietsScore = keyboard.nextInt( ); int highestScore = UtilityClass.max(tomsScore, dicksScore, harrietsScore); System.out.println("Highest score = " + highestScore); } Enter scores for Tom, Dick, and Harriet: 55 100 99 Highest score = 100 CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p371’s exercises 16 - 17. CSS161: Fundamentals of Computing

Privacy Leaks with Array Instance Variables Just as when an accessor returns a reference to any private object public double[] getArray() { return anArray;//BAD! } The example above will result in a privacy leak CSS161: Fundamentals of Computing

Privacy-Leak Preventive Example 1 The previous accessor method would simply return a reference to the array anArray itself Instead, an accessor method should return a reference to a deep copy of the private array object Below, both a and count are instance variables of the class containing the getArray method public double[] getArray() { double[] temp = new double[count]; for (int i = 0; i < count; i++) temp[i] = a[i]; return temp } CSS161: Fundamentals of Computing

Privacy-Leak Preventive Example 2 If a private instance variable is an array that has a class as its base type, then copies must be made of each class object in the array when the array is copied: public ClassType[] getArray() { ClassType[] temp = new ClassType[count]; for (int i = 0; i < count; i++) temp[i] = new ClassType(someArray[i]); return temp; } CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on textbook p375’s exercises 18 ~ 19. CSS161: Fundamentals of Computing