Java Coding 6
David Davenport
Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.
email: [email protected]
Collections
IMPORTANT… Students…
This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn!
Instructors…You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it.
thank you, David.
MusicCD Class ExampleMusicCD( title, artist, tracks)
String getTitle()String getArtist()Track getTrack(int)int getDuration()Date getReleaseDate()
String titleString ArtistDate releaseDate??? tracks
Date( String)String toString()int yearint monthint day
Track( title, length)String getTitle()int getLength()String titleint length
collection of
Easy Collections Use Java’s ArrayList class
ArrayList()boolean add(Object)void add(int, Object)int size()Object get(int)Object remove(int)Object set(int, Object)Object clone()String toString()boolean equals( Object)
ArrayList An object of the ArrayList class
provides a container which can hold any
number of other objects… NEAT!
Objects arranged in a sequence: LIST
[ milk, eggs, bread, honey ]
Easy Problem Read in a set of positive integer values and then
print out a table showing the average, each of the values and their difference from the average.
Average is 5
Value Diff--------------10 53 -26 11 -4--------------
Example output…
Umm… must remember all the values we read in
in order to print the table.
Could use ArrayList… BUTintegers are not Objects!
(use Integer wrapper class)
Not-so-easy Collections Arrays
Common data structure All elements of same type Are Objects in Java Basis of ArrayList class!
3 6 5 1101 2 3 40
grades
Each element has unique successor & predecessor
(except first & last.)
Each element identified by an index (label/subscript)
Name for entire structure
Array Syntax (1)
Arrays are Objects, so declare variable then instantiate
3 6 5 1101 2 3 40
grades
type[] variableName ;
variableName = new type[ noOfElements ];
int[] grades;grades = new int[5];
Note use of square brackets!
Array size cannot be changed after
creation!
Array Syntax (2)
Referring to an individual elementvariableName[index]
examples
grades[0] grades[ i]grades[1] grades[ i+1]names[99] names[ FIRST]
grades[0] = 10;grades[1] = grades[0] + 2;System.out.println( grades[0]);names[99] = scan.nextLine();
Where index is a literal, named constant, variable, or expression.
Processing all elements e.g. Printing contents of array grades
for ( int i = 0; i < grades.length; i++)System.out.println( grades[i] );
for each int k in grades arrayprint k
for ( int i = 0; i < ___________; i++)System.out.println( grades[i] );
System.out.println( grades[0] );System.out.println( grades[1] ); :
// alternate for syntaxfor ( int k : grades)
System.out.println( k);
Easy Problem using arrays! Printing table of differences from average
1. read set of values
2. compute average of set of values
3. print table of differences using average & set of values
Steps 2 & 3 are straightforward For step 1 need to know how many values
Fixed, e.g. 5 Ask user Use sentinel - but length of array is fixed!
Data Requirements:average – doublesetOfValues – int[]
Easy Problem with Methods! Identify method signatures from algorithm
1. read set of values
2. compute average of set of values
3. print table of differences using average & set of values
int[] readSetOfValues()
double computeAverage( int[] setOfValues)
void printTable( double average, int[] setOfValues)
Note: Object-type parameters can act as outputs too!
Arrays of objects Array contains only references to objects
Track[] tracks;tracks = new Track[5];
tracks[0] = new Track( “David”, 100);tracks[1] = new Track( “Gunes”, 200);
Still need to create actual objects
1 2 3 40
tracks
David100
Gunes200
tracks[0].getTitle()
tracks[4].getTitle()