Seminar01 Generic Collections

  • Published on
    11-Dec-2015

  • View
    225

  • Download
    7

Embed Size (px)

DESCRIPTION

thuc hanh lan 1

Transcript

  • Lp trnh ng dng Java 14HCB

    Seminar 01: Generic Collection

  • Ni dung

    Thng tin mn hc Java Collections Java Generics Bi tp

    HTThanh @ FIT-HCMUS 2

  • THNG TIN MN HCH Tun Thanh

    HTThanh @ FIT-HCMUS 3

  • Thng tin

    GVLT: p Thy Nguyn Vn Khit

    nvkhiet@fit.hcmus.edu.vn

    GV HDTH:p Thy Trng Phc Lc

    tploc@fit.hcmus.edu.vnp Thy H Tun Thanh

    htthanh@fit.hcmus.edu.vn

    HTThanh @ FIT-HCMUS 4

  • Lu

    Khng xin SDT ca GV Gp GV:p Gi email xin gpp ngh ra vi option GV quyt nhp Nhn confirm t GVp I82, g ca bc vo

    Gi email:p Tiu : r rngp Ni dung: MSSV, H tn, Lp, nu r l do gip CC cho cc GV trong mn hc

    Trao i hc tp:p Moodle

    HTThanh @ FIT-HCMUS 5

  • JAVA COLLECTIONSH Tun Thanh

    HTThanh @ FIT-HCMUS 6

  • Mng V d

    HTThanh @ FIT-HCMUS 7

  • Mng Nhn xt

    Phi cho bit trc s lng phn t trong mng

    Ko th thay i kch thc v sau (m rng)p Khai bo mng mi cars3p Copy d liu qua mng mi cars3p Cp vng nh mi cho cars2p Copy d liu t cars3 qua cars2

    Thm phn t x vo v tr k Xa phn t x

    HTThanh @ FIT-HCMUS 8

  • Java Collections

    Collection:p Mt i tng c kh nng cha cc i tng khcp Mt danh sch, mt mng

    Cc thao tc thng dng:p Khi to collectionp Thm mt i tng vo collectionp Xa mt phn t ra khi collectionp Kim trong mt i tng c trong collection kop Ly mt phn t trong collectionp Duyt qua cc phn t trong collection

    Thay i kch thc d dng

    HTThanh @ FIT-HCMUS 9

  • S lp

    HTThanh @ FIT-HCMUS 10

  • Collection Framework

    Interfaces: cc giao tip th hin tnh cht ca cc loi collection khc nhau

    Implementations (Classes): cc lp ci t cc collection interfacep C th to class mi, ci t li interface

    Algorithms: cc phng thc tnh, x l trn collectionp Sp xpp Tm kim

    HTThanh @ FIT-HCMUS 11

  • Interface Collection

    Mc cao nht trong Collection Framework Cc method thng dng:p boolean add(Object obj);p boolean contains(Object obj);p boolean isEmpty( );p Iterator iterator( );p boolean remove(Object obj);p int size( );p Object[ ] toArray( );

    1 class khi khng mun ci t method no UnsupportedOperationException

    HTThanh @ FIT-HCMUS 12

  • Interface List

    K tha interface Collection Cha danh sch cc phn t, truy cp thng qua

    ch s C th cha cc phn t trng nhau Mt s method ring:p Object get(int index);p Object set(int index, Object o);p void add(int index, Object o);p Object remove(int index);p int indexOf(Object o);p int lastIndexOf(Object o);

    p kiu ko ng CastClassException

    HTThanh @ FIT-HCMUS 13

  • Interface Set

    K tha interface Collection Cha tp hp cc phn t (ko trng

    nhau) Mt s method ring:p set1.addAll(set2); // php hip set1.retainAll(set2); // php giaop set1.removeAll(set2); // php tr

    HTThanh @ FIT-HCMUS 14

  • Interface SortedSet

    K tha interface Set Qun l tp hp c xp tng dn Lp i tng phi ci t interface

    Comparable hoc phi truyn vo lp ci t SortedSet mt Comparator

    Mt s phng thc ring:p Object first( );p Object last( );p SortedSet headSet(Object end); // =startp SortedSet subSet(Object start, Object end);

    HTThanh @ FIT-HCMUS 15

  • Interface Queue

    K tha interface Collection Hot ng theo c ch FIFO Mt s method ring:p boolean offer(Object obj); // Thm vo queuep Object poll(); // Xa khi queue, tr v phn

    t up Object peek(); // Ly phn t u, ko xa

    HTThanh @ FIT-HCMUS 16

  • Interface Map

    Mc cao nht trong Collection Framework Ko k tha interface Collection Qun l cc phn t theo c ch key-

    valuep Cc Key khng trng nhau

    HTThanh @ FIT-HCMUS 17

  • Interface Map

    Mt s method thng dng:p Object put(Object key, Object value);p Object get(Object key);p Object remove(Object key);p boolean containsKey(Object key);p boolean containsValue(Object value);p Set keySet(); // Tr v cc keyp Collection values(); // Tr v cc valuep Set entrySet(); // Tr v cc cp key-value

    HTThanh @ FIT-HCMUS 18

  • Interface SortedMap

    K tha interface Map Cha cc phn t c sp xp Mt s method ring:p Object firstKey( );p Object lastKey( );p SortedMap headMap(Object end);p SortedMap tailMap(Object start);p SortedMap subMap(Object start, Object end);

    HTThanh @ FIT-HCMUS 19

  • Interface Iterator

    Duyt qua Collection Mt s phng thc thng dng:p boolean hasNext( );p Object next( );

    HTThanh @ FIT-HCMUS 20

  • Class ArrayList

    Ci t interface List Khi to:p ArrayList( );p ArrayList(int capacity);

    HTThanh @ FIT-HCMUS 21

  • Class ArrayList

    HTThanh @ FIT-HCMUS 22

  • Class LinkedList

    Ci t 2 interface List v Queue Ci t dng danh sch lin kt

    HTThanh @ FIT-HCMUS 23

  • Class LinkedList

    HTThanh @ FIT-HCMUS 24

  • Queue V d

    HTThanh @ FIT-HCMUS 25

  • Class Vector

    Ci t interface List Tng t ArrayList Khi to:p Vector( );p Vector(int size);p Vector(int size, int incr);

    HTThanh @ FIT-HCMUS 26

  • Class Vector

    HTThanh @ FIT-HCMUS 27

  • Class Stack

    K tha class Vector Hot ng theo c ch LIFO Mt s method thng dng:p boolean empty() ;p Object peek( );p Object pop( );p Object push(Object element);

    HTThanh @ FIT-HCMUS 28

  • Class Stack

    HTThanh @ FIT-HCMUS 29

  • Class HashSet

    Ci t interface Set

    HTThanh @ FIT-HCMUS 30

  • Class TreeSet

    Ci t SortedSet

    HTThanh @ FIT-HCMUS 31

  • Class TreeSet

    HTThanh @ FIT-HCMUS 32

    class Student implements Comparable{

    private String code;private double score;

    public Student(String code, double score){

    this.code = code;this.score = score;

    }

    public double getScore(){

    return score;}

    public String toString(){

    return "(" + code + "," + score + ")";}

  • Class TreeSet

    HTThanh @ FIT-HCMUS 33

    public boolean equals(Object other){

    Student otherStu = (Student) other;return code.equals(otherStu.code);

    }

    public int compareTo(Object other){

    Student otherStu = (Student) other;return code.compareTo(otherStu.code);

    }}

  • Class TreeSet

    HTThanh @ FIT-HCMUS 34

    // This programs sorts a set of students by name and then by scoreimport java.util.*;

    public class TreeSetTest2{

    public static void main(String[] args){

    SortedSet stu = new TreeSet();stu.add(new Student("A05726", 8.5));stu.add(new Student("A06338", 7.0));stu.add(new Student("A05379", 7.5));stu.add(new Student("A06178", 9.5));

    System.out.println(stu);

    SortedSet sortByScore = new TreeSet(new Comparator()// create an inner class

  • Class TreeSet

    HTThanh @ FIT-HCMUS 35

    { public int compare(Object a, Object b){

    Student itemA = (Student) a;Student itemB = (Student) b;double scoreA = itemA.getScore();double scoreB = itemB.getScore();

    if ( scoreA < scoreB )return -1;

    elsereturn 1;

    }}); // end of inner class

    sortByScore.addAll(stu);System.out.println(sortByScore);

    }}

  • Class HashMap

    Ci t interface Map

    HTThanh @ FIT-HCMUS 36

  • Class TreeMap

    Ci t SortedMap

    HTThanh @ FIT-HCMUS 37

  • Algorithm

    HTThanh @ FIT-HCMUS 38

  • Algorithm

    HTThanh @ FIT-HCMUS 39

  • JAVA GENERICSH Tun Thanh

    HTThanh @ FIT-HCMUS 40

  • t vn

    HTThanh @ FIT-HCMUS 41

  • Khc phc

    HTThanh @ FIT-HCMUS 42

  • Li khuyn

    HTThanh @ FIT-HCMUS 43

  • Generic Class

    HTThanh @ FIT-HCMUS 44

  • Generic Function

    HTThanh @ FIT-HCMUS 45

    Output

  • Wildcards

    HTThanh @ FIT-HCMUS 46

    ?

    ? extends type

    ? super type

  • Ti liu tham kho

    Slide Java Collections Professor Evan Korth

    Slide Java Generics & Collections thy Nguyn L Hong Dng

    http://www.tutorialspoint.com/java/java_collections.htm

    http://www.tutorialspoint.com/java/java_generics.htm

    HTThanh @ FIT-HCMUS 47

  • BI TPH Tun Thanh

    48HTThanh @ FIT-HCMUS

  • Bi tp

    Bi tp c nhn Vit chng trnh ng dng WinForm

    1. Hin th danh sch sinh vin2. Tm sinh vin theo t nht 2 tiu ch (ngi

    dng c th nhp vo 1 hoc 2 tiu ch)3. Thm mi sinh vin4. Cp nht thng tin sinh vin5. Xa thng tin sinh vin

    49HTThanh @ FIT-HCMUS

  • Bi tp

    Yu cu k thutp Javap Lu tr plain text filep Swingp Generic, collections

    Bi np: MSSV.rar/zipp Checklist.txt: t chm im cho 5 chc nng trn,

    mi chc nng thang 10p Th mc Source code: cha ton b m ngunp Th mc Data: cha file d liu, sinhvien.txtp Th mc Demo: hnh nh demo cc chc nng, mi

    chc nng c th chp >=1 hnh, t STT (1-5) ngp Th mc EXE: cha file jar chy c

    50HTThanh @ FIT-HCMUS

  • HTThanh @ FIT-HCMUS 51