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

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
CS0007: Introduction to Computer Programming Array Algorithms.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 8 Arrays and Strings
Chapter 7: Working with Arrays
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Java Coding 6 David Davenport Computer Eng. Dept.,
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Introduction To Programming Information Technology , 1’st Semester
Java Coding 6-extra David Davenport Computer Eng. Dept.,
CS2011 Introduction to Programming I Arrays (I)
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
CSE 143 Lecture 2 ArrayIntList, binary search
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Week 7 - Monday CS 121.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. 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 saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

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

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, … ]

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

Not-so-easy Collections Arrays Common data structure All elements of same type Are Objects in Java Basis of ArrayList class! 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 grades type[] variableName ; variableName = new type[ noOfElements ]; int[] grades; grades = new int[5]; Note use of square brackets!

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();

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] ); :

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

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

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? grades in-usenot in-use One simple & common solution

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

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)

Sorting Selection sort n=6n=5n=4n=3n=2n=1 Sum = n.( n +1) / 2 = n 2 /2 + n/ O( n 2 )

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 grades tmp swap( int i, int j) { int tmp; tmp = X[i]; X[i} = X[j]: X[j] = tmp; }