Typeparametrisering Generel kode (generisk kode).

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
Advertisements

Operator overloading redefine the operations of operators
For(int i = 1; i
Comparable and Comparator Interfaces Ben Rodes. Disclaimer.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Priority Queues1 Priority Queues (Antrian Berprioritas)
Android Activity Class (Android Activities) Brugergrænseflade og brugeraktioner er en aktivitet // ** The Activity Class ******************************************
Composite: Shapes Organisation State Observer Design Patterns.
FEN IntroJava2006 AAU1 Nedarvning Specialisering/Generalisering Subklasse/Superklasse Statisk/Dynamisk type Polymorfi Interfaces.
1 | 2010 Activity og GUI Android Brugergrænseflade.
ADO.NET (and more LINQ) Entity Framework (and more LINQ) Verification (CodeContracts) TPL (Task Parallel Library) Other.NET/C# topics.
Data Structures and Collections
Implimentering af IComparable interfacet class Cirkel: IComparable { private long radius; // object memory :::::::::::::::::::::::::::::::::::::: #region.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
C#: Udtryk og metoder. Indhold “With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…” Udtryk.
Grundlæggende web design, ITU Torsdag d. 29 sept Cascading Style Sheets.
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
1/03/09 De 89 à 98. 1/03/09 De 89 à 98 1/03/09 De 89 à 98.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
15-Jun-15 Lists in Java Part of the Collections Framework.
Modellering af iTunes Oplæg til næste uges øvelses- og afleveringsopgave.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
1 L42 Collections (2). 2 OBJECTIVES  To use collections framework algorithms to manipulate  search  sort  and fill collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Jesper Mosegaard Multimedie Programmering E2003 MMProg uge44 Lingo grafik og animation.
Selection Sort
12-Jul-15 Lists in Java Part of the Collections Framework.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Building Java Programs Interfaces, Comparable reading: , 16.4, 10.2.
Advanced Java Session 3 New York University School of Continuing and Professional Studies.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Building Java Programs Chapter 13 Searching reading: 13.3.

Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 9. Disjoint Sets.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Collections –data structures and Algorithms L. Grewe.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Collection 105 Yola. To store data in RAM Variables (name all the types with their length) Arrays (one, two or more) Collections and Maps.
The Java Collections Framework Based on
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Selection Sort
FOOT OF THE SLOPE DETERMINED BY MEANS OF EVIDENCE TO THE CONTRARY (CHAPTER 6) Harald Brekke.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
1 MIPS Assembly Language Programming CDA 3101 Discussion Section 04.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j

The Selection Algorithm : Design & Analysis [10].
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
。 33 投资环境 3 开阔视野 提升竞争力 。 3 嘉峪关市概况 。 3 。 3 嘉峪关是一座新兴的工业旅游城市,因关得名,因企设市,是长城文化与丝路文化交 汇点,是全国唯一一座以长城关隘命名的城市。嘉峪关关城位于祁连山、黑山之间。 1965 年建市,下辖雄关区、镜铁区、长城区, 全市总面积 2935.
תכנות מכוון עצמים ושפת JAVA
Isam Saeed, Masjid Næstved Hvad er respekt? Hvordan behandler du andre Det er hensyn til folks følelser Det anerkender værdien af mennesker, ejendele,
20171 Atomic energy levels Blackman 3RD p Chapter 4.
Java Algorithms.
Building Java Programs
slides created by Marty Stepp
Serious Privacy Game Workshop
Running Time Exercises
Part of the Collections Framework
Sorting Algorithms.
Presentation transcript:

Typeparametrisering Generel kode (generisk kode)

Et lille eksempel med strenge dIntProg, E10

Forprogrammerede metoder Klassen Collections indeholder en lang række anvendelige metoder: int binarySearch(List l, T key) void copy(List dest, List src) boolean disjoint(Collection c1, Collection c2) int frequency(Collection c, Object o) T max(Collection c) T min(Collection c) void reverse(List l) void shuffle(List l) void sort(List l)...

Collections funktioner dIntProg, E10 Som hovedregel forudsætter metoderne at typen T definerer en total ordning f.eks. ved at elementerne implementerer interfacet Comparable

Korteste Track // Vi antager at listen tracks ikke er tom public Track shortestTrack(){ Track res; res= tracks.get(0); //res == min element so far for ( Track t : tracks ){ if ( t.getTime() < res.getTime() ) { res= t; } return res; } dIntProg, E10

Yngste Person // Vi antager at listen persons ikke er tom public Person youngestPerson(){ Person res; res= persons.get(0); //res == min element so far for ( Person p : persons ){ if ( p.getAge() < res.getAge() ) { res= p; } return res; } dIntProg, E10

Hvad er forskellen på de to metoder? public Person youngestPerson(){ Person res= persons.get(0); //res == min element so far for ( Person p : persons ){ if ( p.getAge() < res.getAge() ) { res= p; } } return res; } public Track shortestTrack(){ Track res= tracks.get(0); //res == min element so far for ( Track t : tracks ){ if ( t.getTime() < res.getTime() ) { res= t; } } return res; } dIntProg, E10

Hvordan generaliserer vi kode? public void square100() { for ( int i= 0; i<4; i++ ) { move(100); turn(90); } public void square200() { for ( int i= 0; i<4; i++ ) { move(200); turn(90); } public void triangle100() { for ( int i= 0; i<3; i++ ) { move(100); turn(120); } public void triangle400() { for ( int i= 0; i<3; i++ ) { move(400); turn(120); } dIntProg, E10

Kan vi generalisere disse metoder? public Person youngestPerson(){ Person res= persons.get(0); //res == min element so far for ( Person p : persons ){ if ( p.getAge() < res.getAge() ) { res= p; } } return res; } public Track shortestTrack(){ Track res= tracks.get(0); //res == min element so far for ( Track t : tracks ){ if ( t.getTime() < res.getTime() ) { res= t; } } return res; } dIntProg, E10

Vi ønsker at kunne... // Vi antager at listen l ikke er tom public T min(List l) { T res; res= l.get(0); // res == min element so far for ( T e : l ) { if ( [sammenligning af e og res]) { res= e; } return res; }...parametrisere med elementtype...parametrisere med sammenligningsmetode dIntProg, E10

Hvordan sammenligner man objekter? public interface Comparable { /** whether this object is * smaller (negative integer) * equal (0) * or greater (positive integer) * than object o */ public int compareTo(T o); } dIntProg, E10

Brug af interfaces (implementør) Tænk på et interface som en rolle Objekter fra en klasse kan spille rollen beskrevet i et interface –Track-objekter kan spille rollen Comparable public class Track implements Comparable {... public int compareTo(Track o) {... } dIntProg, E10

Afkobling af programkomponenter Collections T min(Collection c) void sort (List l)... > Comparable Person Terning Track dIntProg, E10