DOC PREVIEW
UMD CMSC 131 - Lecture 18: Arrays

This preview shows page 1-2 out of 5 pages.

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

Unformatted text preview:

Lecture 18 Arrays Last time 1 switch 2 break 3 Case continuation or fall through 4 break continue of loops This lecture set 1 Intro to arrays 2 Copying arrays and making arrays bigger 3 Array lengths and out of bounds indexing 4 Passing arrays and array elements to a function CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr Data Structures and Arrays Data structures mechanisms for storing data in a structured way We have seen simple data structures implemented as classes Rational java Rational number data stored as numerator denominator pair Arrays are a very useful data structure provided by Java and other programming languages Array sequence of variables of the same type homogeneous data structure size quantity fixed when space is allocated ordered Individual elements of sequence can be referenced updated etc Arrays are objects hence allocated on heap with a reference on the stack Like other objects instance variables of array cells in array are assigned default values 0 null etc when array created CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 1 Array Indexing Java provides a special syntax for uniformly accessing cells in an array Declaration of a int a Allocation of space for array named a a new int 5 This creates five int variables named a 0 a 1 a 2 a 3 a 4 To modify contents of cell 2 to 6 and cell 1 to 74 a 2 6 a 1 74 To use the contents of cell 2 and cell 1 System out println value a 1 a 2 This access mechanism to the individual elements is called array indexing In Java C C array cells are indexed beginning at 0 and going up to n 1 n is number of cells Beware start at 0 and end at one less than the size CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 2 1 Square Brackets and length Three uses in Java Array variable declaration int a Array object creation new int 10 Array indexing a 0 array also has a length holds the amount of space currently allocated for that array CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 3 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 2007 Jan Plane adapted from Bonnie Dorr 4 Summary of Arrays Arrays are Sequences of cells holding values of the same type base type Objects hence created using new To define an array variable To create an array object int a an array with base type int 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 2007 Jan Plane adapted from Bonnie Dorr 5 2 A Common Programming Idiom To process all elements in array a Do following for int i 0 i a length i process the one element at a i Use fresh loop counter to avoid overwriting another variable of same name elsewhere Remember Use i a length not i a length CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 6 Copying Arrays Does the following copy a into b int a new int 5 int b a No a b are aliases How to make a copy For now use loop int a new int 5 int b new int a length for int i 0 i a length i b i a i CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 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 2007 Jan Plane adapted from Bonnie Dorr 8 3 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 2007 Jan Plane adapted from Bonnie Dorr 9 Arrays As Arguments Arrays objects Array variables references Array cells variables of the base type references or primitives depending on what that base type is Both can be used as arguments to methods Array cells passed just like the variables of that base type Array arguments passed just like objects Reference to array is passed in If the method expects an array of doubles an array of doubles of any size can be passed Promotion does not apply You cannot pass an int array CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 10 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 See Array Example 3 5 CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 0 1 2 11 4 Arrays of Objects Class types can also be base types of arrays e g String acc new String 3 Array cells store references to objects Stack Heap acc Array initializers can also be used String acc UMD UNC Duke UMD UNC Duke CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 12 Arrays of Objects continued Class types can also be base types of arrays e g String acc new String 3 Stack Heap kennel Array cells store references to objects Array initializers can also be used More complicated example than strings Cat objects Expressions can also appear in initializers String acc UMD UNC Duke Cat kennel new Cat Joe new Cat Jill new Cat Fluffy Joe Jill Fluffy CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 13 5


View Full Document

UMD CMSC 131 - Lecture 18: Arrays

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 18: Arrays
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 18: Arrays 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 18: Arrays 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?