Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.

Similar presentations


Presentation on theme: "Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections."— Presentation transcript:

1 Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Collections

2 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. David

3 MusicCD Class Example MusicCD( title, artist, tracks) StringgetTitle() StringgetArtist() TrackgetTrack(int) intgetDuration() DategetReleaseDate() Stringtitle StringArtist DatereleaseDate ???tracks Date( String) StringtoString() intyear intmonth intday Track( title, length) StringgetTitle() intgetLength() Stringtitle intlength collection of

4 Easy Collections Use Java’s ArrayList class ArrayList() booleanadd(Object) voidadd(int, Object) intsize() Objectget(int) Objectremove(int) Objectset(int, Object) Objectclone() StringtoString() booleanequals( 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, … ]

5 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 ValueDiff -------------- 105 3-2 61 1-4 -------------- Example output… Umm… must remember all the values we read in in order to print the table. Could use ArrayList… BUT integers are not Objects! (use Integer wrapper class)

6 Not-so-easy Collections Arrays Common data structure All elements of same type Are Objects in Java Basis of ArrayList class! 365110 12340 grades Each element has unique successor & predecessor (except first & last.) Each element identified by an index (label/subscript) Name for entire structure

7 Array Syntax (1) Arrays are Objects, so declare variable then instantiate 365110 12340 grades type[] variableName ; variableName = new type[ noOfElements ]; int[] grades; grades = new int[5]; Note use of square brackets!

8 Array Syntax (2) Referring to an individual element variableName[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] = Keyboard.readString();

9 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 element i in array grades print contents of grades element i for ( int i = 0; i < ___________; i++) System.out.println( grades[i] ); System.out.println( grades[0] ); System.out.println( grades[1] ); :

10 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 For step 1 need to know how many values Fixed, e.g. 5 Ask user Use sentinel - but length of array is fixed! Steps 2 & 3 are straightforward

11 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 12340 tracks David 100 Gunes 200 tracks[0].getTitle() tracks[4].getTitle()

12 Using part of an array (1) Array size specified & fixed at instantiation Problem if required size is unknown? Solution make big enough for worst-case & use part of it Must divide array into two sets, in-use & not in-use … but how? 365110 12340 grades 3-57 567 in-usenot in-use One simple & common solution

13 Using part of an array (2) Store elements sequentially from element zero Keep count of number of in-use elements (valid) 365110 12340 grades ??? 567 in-usenot in-use 5 valid 8 maxEls Now process only valid elements not maxEls

14 Searching Search the first n elements of x for a target value & return its location if found, else -1 public static int search(int n, int[] x, int target) { int pos = 0; while ( pos < n && x[pos] != target) pos++; if ( x[pos] == target) return pos;// found at pos else return –1;// not found } Sequential search O(n)

15 Sorting Selection sort 591734591734 541739541739 541379541379 341579341579 314579314579 134579134579 n=6n=5n=4n=3n=2n=1 Sum = n.( n +1) / 2 = n 2 /2 + n/2 +++++ O( n 2 )

16 Selection Sort To sort first n elements of array X while n > 1 find location of max value in first n elements of X swap element at location of max with element at n-1 decrement n 91735 12340 grades tmp 9 3 9 swap( int i, int j) { int tmp; tmp = X[i]; X[i} = X[j]: X[j] = tmp; }

17


Download ppt "Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections."

Similar presentations


Ads by Google