Java Programming: Arrays and Their Usage, Study notes of Computer Science

An excerpt from the 'intro to programming ii' course at the university of san francisco. It covers the basics of arrays in java, including their declaration, allocation, indexing, and usage in various scenarios. The document also includes examples and exercises to help students understand the concepts.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-18d-1
koofers-user-18d-1 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intro to Programming II
Arrays in Java
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science University of San Francisco p.1/??
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Programming: Arrays and Their Usage and more Study notes Computer Science in PDF only on Docsity!

Intro 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-0:^ Arrays and Collections

Many times in a program, you will need to have acollection of 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 look at Lists, then Treesand Hashtables.

Department of Computer Science — University of San Francisco – p.2/

8-2:^ Arrays

We need to use ’new’ to actually allocate memory for thearray. int^ x^ = sc.nextInt();atBats^ =^ new^ int[10];runs^ =^ new^ int[x]; This means that the reference to the array will beallocated on the stack, while the array itself will beallocated on the heap.

Department of Computer Science — University of San Francisco – p.4/

8-3:^ Tracing arrays

What would a trace of this look like? public^ class^ Arraytest^

{ public static void main^ { int myArray[];myArray = new int[10];System.out.println(myArray[4]); } }

Department of Computer Science — University of San Francisco – p.5/

8-5:^ Indexing an Array

One of the most common things to do with an array is loopover 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; } Notice that arrays have a length data memeber - this isdifferent from length() with strings.

Department of Computer Science — University of San Francisco – p.7/

8-6:^ Initializing Arrays

If you know the elements in your array ahead of time, youcan initialize them like this: String^ daysOfWeek[]^ = {‘‘Monday’’,’’Tuesday’’,’’Wednesday’’,’’Thursday’’,’’Friday’’,’’Saturday’’,’’Sunday’’

}; This is convenient when you have a sequence of values touse as constants. (days, months, colors, grades, etc)

Department of Computer Science — University of San Francisco – p.8/

8-8:^ Method calls with arrays

The method signature contains the array name andbrackets - 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 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.10/

8-9:^ In-class exercise, pt 1

Find your Student class that we built previously.^ Modify it so that it has three instance variables: name,ID, GPA and an appropriate 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 ofthe array.

Department of Computer Science — University of San Francisco – p.11/

8-11:^ 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-12:^ 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/