Seminar01 Generic Collections

  • Published on
    11-Dec-2015

  • View
    225

  • Download
    7

Embed Size (px)

DESCRIPTION

thuc hanh lan 1

Transcript

<ul><li><p>Lp trnh ng dng Java 14HCB</p><p>Seminar 01: Generic Collection </p></li><li><p>Ni dung</p><p> Thng tin mn hc Java Collections Java Generics Bi tp</p><p>HTThanh @ FIT-HCMUS 2</p></li><li><p>THNG TIN MN HCH Tun Thanh</p><p>HTThanh @ FIT-HCMUS 3</p></li><li><p>Thng tin</p><p> GVLT: p Thy Nguyn Vn Khit</p><p> nvkhiet@fit.hcmus.edu.vn</p><p> GV HDTH:p Thy Trng Phc Lc</p><p> tploc@fit.hcmus.edu.vnp Thy H Tun Thanh</p><p> htthanh@fit.hcmus.edu.vn</p><p>HTThanh @ FIT-HCMUS 4</p></li><li><p>Lu </p><p> 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</p><p> Gi email:p Tiu : r rngp Ni dung: MSSV, H tn, Lp, nu r l do gip CC cho cc GV trong mn hc</p><p> Trao i hc tp:p Moodle</p><p>HTThanh @ FIT-HCMUS 5</p></li><li><p>JAVA COLLECTIONSH Tun Thanh</p><p>HTThanh @ FIT-HCMUS 6</p></li><li><p>Mng V d</p><p>HTThanh @ FIT-HCMUS 7</p></li><li><p>Mng Nhn xt</p><p> Phi cho bit trc s lng phn t trong mng</p><p> 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</p><p> Thm phn t x vo v tr k Xa phn t x</p><p>HTThanh @ FIT-HCMUS 8</p></li><li><p>Java Collections</p><p> Collection:p Mt i tng c kh nng cha cc i tng khcp Mt danh sch, mt mng</p><p> 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</p><p> Thay i kch thc d dng</p><p>HTThanh @ FIT-HCMUS 9</p></li><li><p>S lp</p><p>HTThanh @ FIT-HCMUS 10</p></li><li><p>Collection Framework</p><p> Interfaces: cc giao tip th hin tnh cht ca cc loi collection khc nhau</p><p> Implementations (Classes): cc lp ci t cc collection interfacep C th to class mi, ci t li interface </p><p> Algorithms: cc phng thc tnh, x l trn collectionp Sp xpp Tm kim</p><p>HTThanh @ FIT-HCMUS 11</p></li><li><p>Interface Collection</p><p> 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( );</p><p> 1 class khi khng mun ci t method no UnsupportedOperationException</p><p>HTThanh @ FIT-HCMUS 12</p></li><li><p>Interface List</p><p> K tha interface Collection Cha danh sch cc phn t, truy cp thng qua </p><p>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><p> p kiu ko ng CastClassException</p><p>HTThanh @ FIT-HCMUS 13</p></li><li><p>Interface Set</p><p> K tha interface Collection Cha tp hp cc phn t (ko trng </p><p>nhau) Mt s method ring:p set1.addAll(set2); // php hip set1.retainAll(set2); // php giaop set1.removeAll(set2); // php tr</p><p>HTThanh @ FIT-HCMUS 14</p></li><li><p>Interface SortedSet</p><p> K tha interface Set Qun l tp hp c xp tng dn Lp i tng phi ci t interface </p><p>Comparable hoc phi truyn vo lp ci t SortedSet mt Comparator</p><p> Mt s phng thc ring:p Object first( );p Object last( );p SortedSet headSet(Object end); // =startp SortedSet subSet(Object start, Object end);</p><p>HTThanh @ FIT-HCMUS 15</p></li><li><p>Interface Queue</p><p> 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 </p><p>t up Object peek(); // Ly phn t u, ko xa</p><p>HTThanh @ FIT-HCMUS 16</p></li><li><p>Interface Map</p><p> Mc cao nht trong Collection Framework Ko k tha interface Collection Qun l cc phn t theo c ch key-</p><p>valuep Cc Key khng trng nhau</p><p>HTThanh @ FIT-HCMUS 17</p></li><li><p>Interface Map</p><p> 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</p><p>HTThanh @ FIT-HCMUS 18</p></li><li><p>Interface SortedMap</p><p> 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);</p><p>HTThanh @ FIT-HCMUS 19</p></li><li><p>Interface Iterator</p><p> Duyt qua Collection Mt s phng thc thng dng:p boolean hasNext( );p Object next( );</p><p>HTThanh @ FIT-HCMUS 20</p></li><li><p>Class ArrayList</p><p> Ci t interface List Khi to:p ArrayList( );p ArrayList(int capacity);</p><p>HTThanh @ FIT-HCMUS 21</p></li><li><p>Class ArrayList</p><p>HTThanh @ FIT-HCMUS 22</p></li><li><p>Class LinkedList</p><p> Ci t 2 interface List v Queue Ci t dng danh sch lin kt</p><p>HTThanh @ FIT-HCMUS 23</p></li><li><p>Class LinkedList</p><p>HTThanh @ FIT-HCMUS 24</p></li><li><p>Queue V d</p><p>HTThanh @ FIT-HCMUS 25</p></li><li><p>Class Vector</p><p> Ci t interface List Tng t ArrayList Khi to:p Vector( );p Vector(int size);p Vector(int size, int incr);</p><p>HTThanh @ FIT-HCMUS 26</p></li><li><p>Class Vector</p><p>HTThanh @ FIT-HCMUS 27</p></li><li><p>Class Stack</p><p> 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);</p><p>HTThanh @ FIT-HCMUS 28</p></li><li><p>Class Stack</p><p>HTThanh @ FIT-HCMUS 29</p></li><li><p>Class HashSet</p><p> Ci t interface Set</p><p>HTThanh @ FIT-HCMUS 30</p></li><li><p>Class TreeSet</p><p> Ci t SortedSet</p><p>HTThanh @ FIT-HCMUS 31</p></li><li><p>Class TreeSet</p><p>HTThanh @ FIT-HCMUS 32</p><p>class Student implements Comparable{ </p><p>private String code;private double score;</p><p>public Student(String code, double score){ </p><p>this.code = code;this.score = score;</p><p>}</p><p>public double getScore(){ </p><p>return score;}</p><p>public String toString(){ </p><p>return "(" + code + "," + score + ")";}</p></li><li><p>Class TreeSet</p><p>HTThanh @ FIT-HCMUS 33</p><p>public boolean equals(Object other){ </p><p>Student otherStu = (Student) other;return code.equals(otherStu.code);</p><p>}</p><p>public int compareTo(Object other){ </p><p>Student otherStu = (Student) other;return code.compareTo(otherStu.code);</p><p>}}</p></li><li><p>Class TreeSet</p><p>HTThanh @ FIT-HCMUS 34</p><p>// This programs sorts a set of students by name and then by scoreimport java.util.*;</p><p>public class TreeSetTest2{ </p><p>public static void main(String[] args){ </p><p>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));</p><p>System.out.println(stu);</p><p>SortedSet sortByScore = new TreeSet(new Comparator()// create an inner class</p></li><li><p>Class TreeSet</p><p>HTThanh @ FIT-HCMUS 35</p><p>{ public int compare(Object a, Object b){ </p><p>Student itemA = (Student) a;Student itemB = (Student) b;double scoreA = itemA.getScore();double scoreB = itemB.getScore(); </p><p>if ( scoreA &lt; scoreB )return -1;</p><p>elsereturn 1;</p><p>}}); // end of inner class</p><p>sortByScore.addAll(stu);System.out.println(sortByScore);</p><p>}}</p></li><li><p>Class HashMap</p><p> Ci t interface Map</p><p>HTThanh @ FIT-HCMUS 36</p></li><li><p>Class TreeMap</p><p> Ci t SortedMap</p><p>HTThanh @ FIT-HCMUS 37</p></li><li><p>Algorithm</p><p>HTThanh @ FIT-HCMUS 38</p></li><li><p>Algorithm</p><p>HTThanh @ FIT-HCMUS 39</p></li><li><p>JAVA GENERICSH Tun Thanh</p><p>HTThanh @ FIT-HCMUS 40</p></li><li><p>t vn </p><p>HTThanh @ FIT-HCMUS 41</p></li><li><p>Khc phc</p><p>HTThanh @ FIT-HCMUS 42</p></li><li><p>Li khuyn</p><p>HTThanh @ FIT-HCMUS 43</p></li><li><p>Generic Class</p><p>HTThanh @ FIT-HCMUS 44</p></li><li><p>Generic Function</p><p>HTThanh @ FIT-HCMUS 45</p><p>Output</p></li><li><p>Wildcards</p><p>HTThanh @ FIT-HCMUS 46</p><p>?</p><p>? extends type</p><p>? super type</p></li><li><p>Ti liu tham kho</p><p> Slide Java Collections Professor Evan Korth</p><p> Slide Java Generics &amp; Collections thy Nguyn L Hong Dng</p><p> http://www.tutorialspoint.com/java/java_collections.htm</p><p> http://www.tutorialspoint.com/java/java_generics.htm</p><p>HTThanh @ FIT-HCMUS 47</p></li><li><p>BI TPH Tun Thanh</p><p>48HTThanh @ FIT-HCMUS</p></li><li><p>Bi tp</p><p> Bi tp c nhn Vit chng trnh ng dng WinForm</p><p>1. Hin th danh sch sinh vin2. Tm sinh vin theo t nht 2 tiu ch (ngi</p><p>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</p><p>49HTThanh @ FIT-HCMUS</p></li><li><p>Bi tp</p><p> Yu cu k thutp Javap Lu tr plain text filep Swingp Generic, collections</p><p> Bi np: MSSV.rar/zipp Checklist.txt: t chm im cho 5 chc nng trn, </p><p>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</p><p>chc nng c th chp &gt;=1 hnh, t STT (1-5) ngp Th mc EXE: cha file jar chy c</p><p>50HTThanh @ FIT-HCMUS</p></li><li><p>HTThanh @ FIT-HCMUS 51</p></li></ul>