









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 15
This page cannot be seen from the preview
Don't miss anything!










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