Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester, 2013-2014.

Slides:



Advertisements
Similar presentations
Class Relationships Part 1: Composition and Association CS 21a: Introduction to Computing I First Semester,
Advertisements

Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Arrays.
Chapter 7 – Arrays.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Class design. int month; int year class Month Defining Classes A class contains data declarations (state) and method declarations (behaviors) Data declarations.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
Previous Exam 1.0. Question 1 - a Is the following statement true or false? Briefly explain your answer. A && B is the same as B && A for any Boolean.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Java Unit 9: Arrays Declaring and Processing Arrays.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Introduction to Object-Oriented Programming
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CSC Programming I Lecture 8 September 9, 2002.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Classes CS 21a: Introduction to Computing I First Semester,
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Introduction to Java Java Translation Program Structure
Lecture 101 CS110 Lecture 10 Thursday, February Announcements –hw4 due tonight –Exam next Tuesday (sample posted) Agenda –questions –what’s on.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields Copyright © 2012 Pearson.
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
Chapter VII: Arrays.
User-Written Functions
Arrays.
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.
Anatomy of a Method.
Arrays and Array Lists CS 21a.
Building Java Programs
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Corresponds with Chapter 5
Arrays.
Presentation transcript:

Arrays and Lists: Handling Infinite Data CS 21a: Introduction to Computing I First Semester,

Problem 1: Reversing Input ► Problem: Read in three numbers and then print out the numbers in reverse order ► Can you think of a straightforward solution?

Problem 1: Reversing Input ► Declare three variables ► Read in the values using a Scanner ► Print them out println( "Numbers:" ); double num1; double num2; double num3; num1 = in.nextDouble(); num2 = in.nextDouble(); num3 = in.nextDouble(); println( "Reverse:" ); println( num3 ); println( num2 ); println( num1 );

Generalizing a Program ► Suppose we wanted the same program but wanted 10 instead of 3 numbers? ► Suppose we wanted to read in 1000 numbers? ► More than 3000 lines of code if we used the same approach! ► Solution: arrays

Arrays ► Definition ► collection of elements of the same type ► each element is accessed through an index ► In Java, ► declaration: double[] nums; ► creation: nums = new double[8]; ► use: nums[3] = 6.6; ► Note: starting index is 0 (0 to 7, for above) double nums[] is also legal, but double[] nums is preferred, since it emphasizes that the type is double[] ("double array" or "array of doubles")

Visualizing an Array Declare: double[] nums; nums null

Visualizing an Array Declare: double[] nums; nums Create: nums = new double[8];

Visualizing an Array Declare: double[] nums; nums Create: nums = new double[8]; Array variables store pointers and are passed by sharing.

Visualizing an Array Declare: double[] nums; nums Create: nums = new double[8]; Use: nums[3] = 6.6;

Visualizing an Array Declare: double[] nums; nums Create: nums = new double[8]; Use: nums[3] = 6.6; Accessing an array element is like accessing an object field.

Reversing 3 Numbers ► Use an array ► declare double[] nums ► create new double[3] ► use indices 0, 1, 2 when referring to the different array elements ► Statements still look redundant (how about using loops?) println( "Numbers:" ); double[] nums; nums = new double[3]; nums[0] = in.nextDouble(); nums[1] = in.nextDouble(); nums[2] = in.nextDouble(); println( "Reverse:" ); println( nums[2] ); println( nums[1] ); println( nums[0] );

Reversing 3 Numbers ► use a for- statement to read in the numbers ► use a for- statement to print them out in reverse println( "Numbers:" ); double[] nums; nums = new double[3]; for( int i = 0; i < 3; i++ ) nums[i] = in.nextDouble(); println( "Reverse:" ); for( int i = 2; i >= 0; i-- ) println( nums[i] );

Reversing 10 Numbers ► Just change the bounds println( "Numbers:" ); double[] nums; nums = new double[10]; for( int i = 0; i < 10; i++ ) nums[i] = in.nextDouble(); println( "Reverse:" ); for( int i = 9; i >= 0; i-- ) println( nums[i] );

First Use of Arrays ► Declare a constant number of data items that are used in a repetitive process. ► Problem can still be solved without arrays, but coding is too tedious without it. ► Becomes more useful as the constant grows larger.

Speaking of Constants… ► Every time you wanted to change the size… ► Need to find and change all places using 10 (including the 9 in the for loop) ► Very tedious and error-prone

Scaling Up ► Use a constant to indicate the array size ► Use that constant in the for loops ► Just need to change one portion of the program when scaling up final int MAX = 10; println( "Numbers:" ); double[] nums; nums = new double[MAX]; for( int i = 0; i < MAX; i++ ) nums[i] = in.nextDouble(); println( "Reverse:" ); for( int i = MAX-1; i >= 0; i-- ) println( nums[i] );

Constants and "Magic Numbers" ► Constants are useful for "magic numbers" – i.e., specific values that are used throughout the code ► e.g., MAX_LENGTH, SCREEN_WIDTH, PI, BLUE, DASHED_LINE, etc. ► Useful because ► makes code more readable and maintainable ► e.g., WHITE is easier to understand and easier to remember than 255 ► makes modifications easier ► e.g., in reversing program example, we just need to change MAX. No need to look for 10 and 9 and change them.

Reversing N Numbers println( "How many to reverse?" ); int N = in.nextInt(); println( "Numbers:" ); double[] nums; nums = new double[N]; for( int i = 0; i < N; i++ ) nums[i] = in.nextDouble(); println( "Reverse:" ); for( int i = N-1; i >= 0; i-- ) println( nums[i] );

Second (More Important) Use of Arrays

Practice Programming Problem

► Sample Input 9 The quick brown fox jumps over the lazy dog 3 quick hedgehog the ► Sample Output 1 6

Practice Programming Problem

Sample Input Sample Output Palindrome Not a palindrome

Problem 2: Large Banks ► Consider the following Bank class that manages multiple BankAccount objects…

Problem 2: Large Banks class Bank { BankAccount alice; BankAccount bob; public void withdraw(String name, double amount) { if(name.equals("Alice")) alice.withdraw(amount); else if(name.equals("Bob")) bob.withdraw(amount); else reportError(); }

Problem 2: Large Banks ► How can we write Bank so it can handle a large, arbitrary number of BankAccounts? getBalance( "Bob" ) withdraw( "Alice", 200 )

Problem 2: Large Banks ► Without arrays, here’s what we need to do every time someone applies for a new bank account. ► Add a new BankAccount field ► Change the if-else code in withdraw, deposit, getBalance, and applyInterest ► Recompile and send our program to the bank who hired us

Solution: Array of Objects ► Declaration BankAccount[] accounts; ► Creation of the Array accounts = new BankAccount[5]; ► creates an array of pointers to BankAccounts ► but no actual BankAccounts yet (initialized with null ) ► Creation of Objects for ( int i = 0; i < 5; i++ ) { accounts[i] = new BankAccount(); } ► creates the BankAccounts themselves and assigns the pointers to the variables

Visualizing an Array of Objects Declare: BankAccount[] accounts; accounts null

Visualizing an Array of Objects Declare: BankAccount[] accounts; accounts Create array: accounts = new BankAccount[5]; null

Visualizing an Array of Objects Declare: BankAccount[] accounts; accounts Create array: accounts = new BankAccount[5]; Create objects: for ( int i = 0; i < 5; i++ ) { accounts[i] = new BankAccount(i * 10); } BankAccount balance 0 BankAccount balance 10 BankAccount balance 20 BankAccount balance 30 BankAccount balance 40

Visualizing an Array of Objects Declare: BankAccount[] accounts; accounts Create array: accounts = new BankAccount[5]; Create objects: for ( int i = 0; i < 5; i++ ) { accounts[i] = new BankAccount(i * 10); } BankAccount balance 0 BankAccount balance 10 BankAccount balance 20 BankAccount balance 30 BankAccount balance 40 Use objects: accounts[3].getBalance(); (returns 30)

Approach ► Include an acctName field in BankAccount ► Add a constructor that allows you to indicate name and balance ► Add a getName method ► Declare an array of BankAccount objects in Bank ► Create the array inside Bank ’s constructor ► Loop through the array to find a matching account before carrying out the transaction ( deposit, withdraw, getBalance )

Approach ► A field representing the account name has been added: acctName ► Constructor that accepts a name and an initial balance ► Get method to access acctName public class BankAccount { private double balance; private String acctName; public BankAccount( String name, double initBalance ) { acctName = name; balance = initBalance; } public String getName() { return acctName; } … }

Approach ► A field representing an array of BankAccount s added: accounts ► There is also a constant representing the maximum number of accounts the bank can handle: MAX ► The array is initialized in the constructor. It is also populated with 2 BankAccount objects named "john" and "marsha" public class Bank { private BankAccount[] accounts; private static final int MAX = 10; public Bank() { accounts = new BankAccount[MAX]; accounts[0] = new BankAccount("john", 100); accounts[1] = new BankAccount("marsha",200); } … }

The deposit Method ► First, loop through the accounts array to find a matching bank account object ► The getName method is used to get the name of an account and compare it with the name argument passed public class Bank {... public void deposit( String name, double amt ) { BankAccount temp = null; for ( int x = 0; x < MAX; x++ ) if ( accounts[x].getName().equals( name ) ) temp = accounts[x]; temp.deposit( amt ); }... }

The deposit Method ► First, loop through the accounts array to find a matching bank account object ► The getName method is used to get the name of an account and compare it with the name argument passed public class Bank {... public void deposit( String name, double amt ) { BankAccount temp = null; for ( int x = 0; x < MAX; x++ ) if ( accounts[x].getName().equals( name ) ) temp = accounts[x]; temp.deposit( amt ); }... } Be careful when writing code like this. Doing this gives a NullPointerException if no BankAccount instance is assigned to that location.

The deposit Method, version 2 public class Bank {... public void deposit( String name, double amt ) { BankAccount temp = null; for ( int x = 0; x < MAX; x++ ) if ( accounts[x] != null ) if ( accounts[x].getName().equals(name) ) temp = accounts[x]; if ( temp != null ) temp.deposit( amt ); }... } check first if the location contains an instance of BankAccount (i.e., not null)

The deposit Method, version 3 public class Bank {... public void deposit( String name, double amt ) { BankAccount temp = null; for ( int x = 0; x < numAccounts; x++ ) if ( accounts[x].getName().equals( name ) ) temp = accounts[x]; if ( temp != null ) temp.deposit( amt ); }... } Another alternative is to change the limit of x to the actual number of accounts the array contains.

Approach ► What is the value of numAccounts ? public class Bank { private BankAccount[] accounts; private static final int MAX = 10; private int numAccounts = 0; public Bank() { accounts = new BankAccount[MAX]; accounts[0] = new BankAccount("john", 100); accounts[1] = new BankAccount("marsha",200); numAccounts = 2; } … }

Creating new BankAccounts public void openAccount( String name, double initbal ) { if ( numAccounts < MAX ) { accounts[numAccounts] = new BankAccount(name, initbal); numAccounts++; } else { println( "Maximum number of accounts reached" ); }

You’ll Sometimes See This Shortcut public void openAccount( String name, double initbal ) { if ( numAccounts < MAX ) accounts[numAccounts++] = new BankAccount(name, initbal); else println( "Maximum number of accounts reached" ); }

Using openAccount as a Convenience Method ► In Bank ’s constructor: public Bank() { accounts = new BankAccount[MAX]; openAccount( "john", 1000 ); openAccount( "marsha", 2000 ); } Better yet, just make calls to openAccount from the driver program, so that a newly created Bank object contains no accounts

The withdraw Method public class Bank {... public void withdraw( String name, double amt ) { BankAccount temp = null; for( int x = 0; x < numAccounts; x++ ) if( accounts[x].getName().equals( name ) ) temp = accounts[x]; if ( temp != null ) temp.withdraw( amt ); }... } Notice that the code is almost identical to the code in the deposit method, except for the last line. How do we eliminate this redundancy?

Using a findAccount public class Bank {... public void deposit( String name, double amt ) { BankAccount temp = findAccount( name ); if ( temp != null ) temp.deposit( amt ); } public void withdraw( String name, double amt ) { BankAccount temp = findAccount( name ); if ( temp != null ) temp.withdraw( amt ); }... } Exercise: write code for the findAccount method and the getBalance method

More about Arrays ► Arrays are objects ► the array variable is just a pointer to the actual array that contains the values ► need to use new after declaring ► passed by sharing ► Special features ► a length field returns the array size ► in recent example, accounts.length would return 10 ► [] operator only work with arrays

More about Arrays ► ArrayIndexOutOfBounds exception ► valid indices for array of size n: 0 to n-1 ► any access to other indices causes an error ► Array size can’t be changed after array is created ► To expand array, we need to create a new array, copy old array contents, then point array variable to new array

Array Initializers ► You can initialize an array with the following syntax: String[] responses = { "Hello", "Hi", "How are you", "How do you do" }; ► Can be used for fields, local variables, and even constants

Useful Pattern ► Put different responses for different cases in an array ► Assign an integer to represent different cases ► In this case 0 means the program will say "Hello", 1 means it will say "Hi", etc. ► Now you can generate the data for each case accordingly ► e.g., What does the following code do? int greetingCase = (int)(Math.random() * responses.length); String greeting = responses[greetingCase] + ", World"; System.out.println( greeting );

Command Line Arguments ► Try this program: public class SayHiTo { public static void main( String[] args ) { System.out.println( "Hi, " + args[0] ); } ► Execute the program outside of BlueJ, through the command line: ► C:\> java SayHiTo Bob

Command Line Arguments ► The String[] args parameter in the main program represents the words you specify in addition to the to java and the program name (e.g., SayHiTo ) ► args[0] refers to the first argument, args[1] refers to the second argument, and so on… ► Use args.length to find out how many arguments are indicated ► In BlueJ, when you after right-click on the Java class and execute main, you may include arguments as well

Multi-dimensional Arrays ► A natural extension of simple (1D) arrays ► 2D declaration: char[][] grid; ► think "array of arrays" ► Array creation grid = new char[10][20]; // 10 rows, 20 columns ► Another way grid = new char[10][]; // creates array of 10 char[]’s for ( int i = 0; i < 10; i++ ) grid[i] = new char[20]; // creates a size-20 array ► This way allows for varying row sizes

Visualizing 2D Arrays Create array of rows: grid = new char[5][]; Declare: char[][] grid; Create rows: for ( int i = 0; i < 5; i++ ) grid[i] = new char[3]; char[][] null char[]-type pointers Use objects: grid[3][2] = 'C' C

Using 2D Arrays ► To refer to individual element, use two indices ► grid[2][1] = 'X'; ► Using only one index refers to a single dimensional array ► grid[4] refers to row 4 ► grid[4].length is the length of row 4 (in this case, it’s 3) ► The array variable by itself refers to the top-level array (i.e., the array of rows) ► grid.length is the length of the array of rows (i.e., it’s the number of rows)

Practice Programming Problem ► Use 2D arrays to create a multiplication table like the following:

Problem 3: Flexible Collections ► How can we write Bank so it can have an arbitrary number of BankAccounts ? ► Right now, with arrays, we can only handle a fixed number of accounts (up to MAX accounts) getBalance( "Bob" ) withdraw( "Alice", 200 )

The Java Collections Framework ► A set of classes that you can use for containing arbitrarily large collections of objects ► To use, you must say import java.util.*; at the top of your code ► Some basic Collections classes ► ArrayList, Vector ► HashMap, Hashtable

ArrayList "Bart" "Lisa" "Maggie" ArrayList names

ArrayList Example import java.util.*; public class ArrayListDemo1 { public static void main( String[] args) { ArrayList names = new ArrayList (); names.add( "Bart" ); names.add( "Lisa" ); names.add( "Maggie" ); for ( int i = 0; i < names.size(); i++ ) { System.out.println( names.get( i ) ); } names.set( 1, "Homer" ); names.add( "Marge" ); for ( int i = 0; i < names.size(); i++ ) { System.out.println( names.get( i ) ); } You have to specify the type of object it has to store. ArrayList 0 "Bart" 1 "Lisa" 2 "Maggie" ArrayList names 3 "Marge" "Homer"

Using Other Types import java.util.*; public class ArrayListDemoWithBankAccounts { public static void main( String[] args) { ArrayList accts = new ArrayList (); accts.add( new BankAccount( "Alice", 2000 ) ); accts.add( new BankAccount( "Bob", 1000 ) ); for ( int i = 0; i < accts.size(); i++ ) { BankAccount curAccount = accts.get( i ); System.out.println( "Bank Account #" + i + "Owner: " + curAccount.getAcctName() + ", " + "Balance: " + curAccount.getBalance() ); } ArrayList ArrayList accts BankAccount double balance 2000 String name "Alice" BankAccount double balance 1000 String name "Bob" 0 1

Looping through ArrayLists ► Using an index … for ( int i = 0; i < accts.size(); i++ ) { BankAccount b = accts.get( i ); System.out.println( b.getBalance() ); } ► Using a for-each loop… for ( BankAccount b : accts ) { System.out.println( b.getBalance() ); } Simpler than a regular for loop. All you have to specify is the object (BankAccount) and the ArrayList (accts).

Practice Programming Problem ► Reverse an arbitrarily long sequence of numbers, where the length is not specified in advance. Hint: Scanner has a hasNext method. Read the docs to see what it does. ► Sample Input ► Sample Output