DOC PREVIEW
UMD CMSC 131 - Lecture 21: Array Operations

This preview shows page 1-2-3-4-5-6 out of 19 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 19 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 21 Array Operations Last time 1 switch 2 break continue 3 Arrays Today 1 Array lengths and out of bounds indexing 2 Copying arrays 3 Explicit array initialization 10 18 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 4 Due Friday The project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants TAs and instructors You must not look at other students code Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Recall Arrays Arrays are Sequences of cells holding values of the same type base type Objects hence created using new To define an array variable int a an array with base type int To create an array object a new int 10 Creates an array of 10 cells The base type is int To access individual array cells use indexing a 0 a 1 a 9 Cells are just like variables They may be read They may be written x a 3 a 2 7 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 More on Array Indexing Recall if a is an array of n elements then Initial element is a 0 Last element is a n 1 Each array as an instance variable length storing the number of elements in the array Consider int a new int 10 System out println a length 10 is printed What happens int a new int 10 a 10 0 Error Exception in thread main java lang ArrayIndexOutOfBoundsException 10 at Sample main Sample java 11 The cells in a are a 0 a 9 a 10 is like an undefined variable This kind of error is called an index out of bounds error If you write a exp and exp evaluates to something a length then Java reports an ArrayIndexOutOfBoundsException error CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Square Brackets Three uses in Java Array variable declaration int a Array object creation new int 10 Array indexing a 0 Don t be confused CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Arrays and Aliasing Arrays are objects so array variables are references What happens int a new int 5 int b null b a b 4 6 System out println a 4 Stack Heap a b 6 is printed a b are aliases Modifying b 4 also modifies a 4 0 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 0 0 0 0 6 5 Array and Garbage What happens Stack int a new int 5 a new int 4 a Heap Garbage a is assigned address of new array object Old array object no longer accessible 0 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 0 0 0 0 0 0 0 0 6 Copying Arrays Does the following copy a into b int a new int 5 int b null b a No a b are aliases How to make a copy For now use loop int a new int 5 int b null b new int a length for int i 0 i a length i b i a i CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Making Arrays Bigger Suppose we want to make an array bigger by adding an element Does the following work int a new int 5 a length No We get the following Exception in thread main java lang Error Unresolved compilation problem The final field array length cannot be assigned at Sample main Sample java 15 a length is immutable No assignment is allowed CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 To Make an Array Bigger Create a new larger array object Copy old array contents into new object Assign address of new object to variable int a new int 5 int temp new int a length 1 for int i 0 i a length i temp i a i a temp New variable temp created to hold copy New block created to ensure temp does not interfere with another variable of the same name Previous contents of a become garbage CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 A Common Programming Idiom To process all elements in array a Do following for int i 0 i a length i a i Use fresh loop counter to avoid overwriting another variable of same name elsewhere Use i a length not i a length why CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Alternate Declaration Syntax To maintain consistency with C C following declaration of array variables also possible int grade Compare to Java standard int grade Java standard generally preferred type emphasizes array status Alternative syntax sometimes handy int grade i gpa Declares two arrays of base type int grade gpa Declares a single int variable i CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Array Initializers Arrays may be initialized at declaration time Stack Heap a int a 5 0 1 2 Java counts elements here 4 creates correct size of array copies elements into array returns reference to array 5 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 0 1 2 12 Example Look at Grade java CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13 Array Arguments Array and array elements can be passed as parameters to methods just like primitive types and object references Passing a Single Element A single element of an array can be passed to any method as if it were a simple variable Example Suppose fooBar z expects an argument z of type double double a new double 10 int i initializations omitted fooBar a 3 passes the value of a 3 to fooBar fooBar a i evaluates a i and passes the value to fooBar Passing an Entire Array You can pass an entire array as an argument As with objects this just passes the reference If the method expects an array of type double then you can pass it an array of doubles of any size Promotion does not apply E g you cannot pass it an array of int s CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 14 Array Arguments Example 1 Formal parameter v Example A static method printDoubles that is given an array of doubles and prints each one per line Prints the ith public static void printDoubles double v System out println Array contents for int i 0 i v length i System out println i v i double height 6 42 7 10 4 83 double weight 122 170 5 printDoubles height Actual printDoubles weight parameter weight CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland element of the array Array contents 0 6 42 1 7 1 2 4 83 Array contents 0 122 0 1 170 5 15 Array Arguments Example 2 Example A static method squareValues that is given an array …


View Full Document

UMD CMSC 131 - Lecture 21: Array Operations

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 21: Array Operations
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 21: Array Operations and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 21: Array Operations and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?