Unformatted text preview:

small lecturenumber - hepage : Arrays and Collectionssmall lecturenumber - hepage : Arrayssmall lecturenumber - hepage : Arrayssmall lecturenumber - hepage : A digression: Memory layoutsmall lecturenumber - hepage : Indexing an Arraysmall lecturenumber - hepage : Indexing an Arraysmall lecturenumber - hepage : Initializing Arrayssmall lecturenumber - hepage : Arrays of Objectssmall lecturenumber - hepage : Method calls with arrayssmall lecturenumber - hepage : In-class exercise, pt 1small lecturenumber - hepage : In-class exercise, pt 2small lecturenumber - hepage : In-class exercise, pt 3small lecturenumber - hepage : Advantages and disadvantages of arrayssmall lecturenumber - hepage : SummaryIntroduction to Programming IIArrays in JavaChris BrooksDepartment of Computer ScienceUniversity of San FranciscoDepartment of Computer Science — University of San Francisco – p.1/??8-2: Arrays and Collections•Many times in a program, you will need to have a collection ofobjects.◦A list of students◦A list of test scores◦A set of data from multiple experiments◦A collection of shapes to draw◦etc•Java has a wide variety of different collection classes.•We’ll start with arrays, and then ArrayLists.◦Later on, Lists, then Trees and Hashtables.Department of Computer Science — University of San Francisco – p.2/??8-3: Arrays•Java provides built-in support for arrays◦An array is a set of objects that are sequentially arranged inmemory.◦Size of the array is declared when it is created.•Example:int []atBats;int runs[]•This declares the array, but not its contents.Department of Computer Science — University of San Francisco – p.3/??8-4: Arrays•We need to use ’new’ to actually allocate memory for the array.int x = sc.nextInt();atBats = new int[10];runs = new int[x];•This means that the reference to the array will be allocated onthe stack, while the array itself will be allocated on the heap.Department of Computer Science — University of San Francisco – p.4/??8-5: A digression: Memory layout•Programs typically use two types of memory:◦Statically allocated memory•These are variables whose type and size can beidentified at compile time.•Usually primitive types.•We say that this memory is allocated “on the stack”◦Dynamically allocated memory•These are variables whose size is determined at run time.•Created with new•We say that this memory is allocated “on the heap.”Department of Computer Science — University of San Francisco – p.5/??8-6: Indexing an Array•One nice feature of arrays is that they can be indexed using the[] operator.◦For example, myArray[4].◦Indices start at 0.◦This is sometimes called random access; we can jumpanywhere in the array we want.Department of Computer Science — University of San Francisco – p.6/??8-7: Indexing an Array•One of the most common things to do with an array is loop overit and do something with all the elements.public double computeAverage(int scores[]) {double ave = 0;for (int i = 0; i < scores.length; i++) {ave = ave + scores[i];}ave = ave / scores.length;return ave;}•Notice that arrays have a length data member - this is differentfrom length() with strings.Department of Computer Science — University of San Francisco – p.7/??8-8: Initializing Arrays•If you know the elements in your array ahead of time, you caninitialize them like this:String daysOfWeek[] ={‘‘Monday’’,’’Tuesday’’,’’Wednesday’’,’’Thursday’’,’’Friday’’,’’Saturday’’,’’Sunday’’};•This is convenient when you have a sequence of values to useas constants. (days, months, colors, grades, etc)Department of Computer Science — University of San Francisco – p.8/??8-9: Arrays of Objects•We can also make arrays of objects.Student studentArray[] = new Student[10];for (int i = 0; i < 10; i++) {studentArray[i] = new Student();}Department of Computer Science — University of San Francisco – p.9/??8-10: Method calls with arrays•The method signature contains the array name and brackets -this indicates that an array is bring passed in.◦double average(int scores[])•When calling a method, just pass the name of the array, as if itwas a regular variable.◦result = average(myscores)•You can also send an element of an array into a method, aslong as the parameter is of the appropriate type.◦int square(int value)◦square(myscores[5])Department of Computer Science — University of San Francisco – p.10/??8-11: In-class exercise, pt 1•Let’s build a Student class◦Give it three instance variables: name, ID, GPA and anappropriate constructor.•Create a program that:◦Prompts the user for a number of students:◦Allocates an array of Students.◦For each student, asks the user their name, ID, GPA.◦Uses the Student constructor to fill in that element of thearray.Department of Computer Science — University of San Francisco – p.11/??8-12: In-class exercise, pt 2•Now modify your program to:◦Compute and display the average GPA for all students.◦Print out a list of all students and their IDs.Department of Computer Science — University of San Francisco – p.12/??8-13: In-class exercise, pt 3•Now, modify your program so that the user can input aminimum and maximum GPA.•Print out the name and ID of all students whose GPA isbetween the minimum and maximum.Department of Computer Science — University of San Francisco – p.13/??8-14: Advantages and disadvantages of arrays•Advantages:◦All memory is contiguous◦Can ’jump’ directly to any element of the array.•Disadvantages:◦Hard to resize or add elements.Department of Computer Science — University of San Francisco – p.14/??8-15: Summary•Arrays let you create a sequential list of objects.•Need to declare array, and then contents with new.•Can iterate over array or access any element using the index.Department of Computer Science — University of San Francisco –


View Full Document

USF CS 112 - Arrays in Java

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Arrays in Java
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 Arrays in Java 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 Arrays in Java 2 2 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?