Unformatted text preview:

8 2 Arrays and Collections Many times in a program you will need to have a collection of Introduction to Programming II Arrays in Java Chris Brooks Department of Computer Science University of San Francisco objects 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 1 8 3 Arrays Department of Computer Science University of San Francisco p 2 8 4 Arrays Java provides built in support for arrays An array is a set of objects that are sequentially arranged in memory Size of the array is declared when it is created Example int atBats int runs 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 on the stack while the array itself will be allocated on the heap This declares the array but not its contents Department of Computer Science University of San Francisco p 3 8 5 A digression Memory layout Department of Computer Science University of San Francisco p 4 8 6 Indexing an Array Programs typically use two types of memory Statically allocated memory These are variables whose type and size can be identified at compile time Usually primitive types We say that this memory is allocated on the stack 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 jump anywhere in the array we want 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 Department of Computer Science University of San Francisco p 6 8 7 Indexing an Array 8 8 Initializing Arrays One of the most common things to do with an array is loop over it 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 If you know the elements in your array ahead of time you can initialize them like this String daysOfWeek Monday Tuesday Wednesday Thursday Friday Saturday Sunday This is convenient when you have a sequence of values to use as constants days months colors grades etc Notice that arrays have a length data member this is different from length with strings Department of Computer Science University of San Francisco p 7 Department of Computer Science University of San Francisco p 8 8 10 Method calls with arrays 8 9 Arrays of Objects The method signature contains the array name and brackets We can also make arrays of objects this indicates that an array is bring passed in double average int scores Student studentArray new Student 10 for int i 0 i 10 i studentArray i new Student When calling a method just pass the name of the array as if it was a regular variable result average myscores You can also send an element of an array into a method as long as the parameter is of the appropriate type int square int value square myscores 5 Department of Computer Science University of San Francisco p 9 8 11 In class exercise pt 1 Department of Computer Science University of San Francisco p 10 8 12 In class exercise pt 2 Let s build a Student class Give it three instance variables name ID GPA and an appropriate constructor Now modify your program to Compute and display the average GPA for all students Print out a list of all students and their IDs 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 the array Department of Computer Science University of San Francisco p 11 Department of Computer Science University of San Francisco p 12 8 13 In class exercise pt 3 8 14 Advantages and disadvantages of arrays Now modify your program so that the user can input a minimum and maximum GPA Print out the name and ID of all students whose GPA is between the minimum and maximum 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 13 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 p 15 Department of Computer Science University of San Francisco p 14


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
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 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?