Docsity

Java Programming: Arrays and Memory Management, Exams of Computer Science

Partial preview of the text

Download Java Programming: Arrays and Memory Management and more Exams Computer Science in PDF only on Docsity!

Introduction to Programming II^ Arrays in Java^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department 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-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-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-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-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/