seminar01 generic collections

51
Lập trình ứng dụng Java 14HCB Seminar 01: Generic – Collection

Upload: nam-le-thanh

Post on 11-Dec-2015

233 views

Category:

Documents


7 download

DESCRIPTION

thuc hanh lan 1

TRANSCRIPT

Page 1: Seminar01 Generic Collections

Lập trình ứng dụng Java 14HCB

Seminar 01: Generic – Collection

Page 2: Seminar01 Generic Collections

Nội dung

£ Thông tin môn học£ Java Collections£ Java Generics£ Bài tập

HTThanh @ FIT-­HCMUS 2

Page 3: Seminar01 Generic Collections

THÔNG TIN MÔN HỌCHồ Tuấn Thanh

HTThanh @ FIT-­HCMUS 3

Page 4: Seminar01 Generic Collections

Thông tin

£ GVLT: p Thầy Nguyễn Văn Khiết

¡ [email protected]

£ GV HDTH:p Thầy Trương Phước Lộc

¡ [email protected] Thầy Hồ Tuấn Thanh

¡ [email protected]

HTThanh @ FIT-­HCMUS 4

Page 5: Seminar01 Generic Collections

Lưu ý

£ Không xin SDT của GV£ Gặp GV:

p Gửi email xin gặpp Đề nghị ra vài option để GV quyết địnhp Nhận confirm từ GVp I82, gõ cửa bước vào

£ Gửi email:p Tiêu đề: rõ ràngp Nội dung: MSSV, Họ tên, Lớp, nêu rõ lý do gửip CC cho các GV trong môn học

£ Trao đổi học tập:p Moodle

HTThanh @ FIT-­HCMUS 5

Page 6: Seminar01 Generic Collections

JAVA COLLECTIONSHồ Tuấn Thanh

HTThanh @ FIT-­HCMUS 6

Page 7: Seminar01 Generic Collections

Mảng – Ví dụ

HTThanh @ FIT-­HCMUS 7

Page 8: Seminar01 Generic Collections

Mảng – Nhận xét

£ Phải cho biết trước số lượng phần tử trong mảng

£ Ko thể thay đổi kích thước về sau (mở rộng)p Khai báo mảng mới cars3p Copy dữ liệu qua mảng mới cars3p Cấp vùng nhớ mới cho cars2p Copy dữ liệu từ cars3 qua cars2

£ Thêm phần tử x vào vị trí k£ Xóa phần tử x

HTThanh @ FIT-­HCMUS 8

Page 9: Seminar01 Generic Collections

Java Collections

£ Collection:p Một đối tượng có khả năng chứa các đối tượng khácp Một danh sách, một mảng

£ Các thao tác thông dụng:p Khởi tạo collectionp Thêm một đối tượng vào collectionp Xóa một phần tử ra khỏi collectionp Kiểm trong một đối tượng có trong collection kop Lấy một phần tử trong collectionp Duyệt qua các phần tử trong collection

£ Thay đổi kích thước dễ dàng

HTThanh @ FIT-­HCMUS 9

Page 10: Seminar01 Generic Collections

Sơ đồ lớp

HTThanh @ FIT-­HCMUS 10

Page 11: Seminar01 Generic Collections

Collection Framework

£ Interfaces: các giao tiếp thể hiện tính chất của các loại collection khác nhau

£ Implementations (Classes): các lớp cài đặt các collection interfacep Có thể tạo class mới, cài đặt lại interface đó

£ Algorithms: các phương thức tĩnh, xử lý trên collectionp Sắp xếpp Tìm kiếm

HTThanh @ FIT-­HCMUS 11

Page 12: Seminar01 Generic Collections

Interface Collection

£ Mức cao nhất trong Collection Framework£ Các method thông dụng:

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 không muốn cài đặt method nào è UnsupportedOperationException

HTThanh @ FIT-­HCMUS 12

Page 13: Seminar01 Generic Collections

Interface List

£ Kế thừa interface Collection£ Chứa danh sách các phần tử, truy cập thông qua chỉ số

£ Có thể chứa các phần tử trùng nhau£ Một số method riêng:

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 kiểu ko đúng è CastClassException

HTThanh @ FIT-­HCMUS 13

Page 14: Seminar01 Generic Collections

Interface Set

£ Kế thừa interface Collection£ Chứa tập hợp các phần tử (ko trùng nhau)

£ Một số method riêng:p set1.addAll(set2);; // phép hộip set1.retainAll(set2);; // phép giaop set1.removeAll(set2);; // phép trừ

HTThanh @ FIT-­HCMUS 14

Page 15: Seminar01 Generic Collections

Interface SortedSet

£ Kế thừa interface Set£ Quản lý tập hợp được xếp tăng dần£ Lớp đối tượng phải cài đặt interface Comparable hoặc phải truyền vào lớp cài đặt SortedSet một Comparator

£ Một số phương thức riêng:p Object first( );;p Object last( );;p SortedSet headSet(Object end);; // <= endp SortedSet tailSet(Object start);; // >=startp SortedSet subSet(Object start, Object end);;

HTThanh @ FIT-­HCMUS 15

Page 16: Seminar01 Generic Collections

Interface Queue

£ Kế thừa interface Collection£ Hoạt động theo cơ chế FIFO£ Một số method riêng:

p boolean offer(Object obj);; // Thêm vào queuep Object poll();; // Xóa khỏi queue, trả về phần tử đầu

p Object peek();; // Lấy phần tử đầu, ko xóa

HTThanh @ FIT-­HCMUS 16

Page 17: Seminar01 Generic Collections

Interface Map

£ Mức cao nhất trong Collection Framework£ Ko kế thừa interface Collection£ Quản lý các phần tử theo cơ chế key-­valuep Các Key không trùng nhau

HTThanh @ FIT-­HCMUS 17

Page 18: Seminar01 Generic Collections

Interface Map

£ Một số method thông dụng: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ề các keyp Collection values();; // Trả về các valuep Set entrySet();; // Trả về các cặp key-­value

HTThanh @ FIT-­HCMUS 18

Page 19: Seminar01 Generic Collections

Interface SortedMap

£ Kế thừa interface Map£ Chứa các phần tử được sắp xếp£ Một số method riêng:

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

Page 20: Seminar01 Generic Collections

Interface Iterator

£ Duyệt qua Collection£ Một số phương thức thông dụng:

p boolean hasNext( );;p Object next( );;

HTThanh @ FIT-­HCMUS 20

Page 21: Seminar01 Generic Collections

Class ArrayList

£ Cài đặt interface List£ Khởi tạo:

p ArrayList( );;p ArrayList(int capacity);;

HTThanh @ FIT-­HCMUS 21

Page 22: Seminar01 Generic Collections

Class ArrayList

HTThanh @ FIT-­HCMUS 22

Page 23: Seminar01 Generic Collections

Class LinkedList

£ Cài đặt 2 interface List và Queue£ Cài đặt dạng danh sách liên kết

HTThanh @ FIT-­HCMUS 23

Page 24: Seminar01 Generic Collections

Class LinkedList

HTThanh @ FIT-­HCMUS 24

Page 25: Seminar01 Generic Collections

Queue – Ví dụ

HTThanh @ FIT-­HCMUS 25

Page 26: Seminar01 Generic Collections

Class Vector

£ Cài đặt interface List£ Tương tự ArrayList£ Khởi tạo:

p Vector( );;p Vector(int size);;p Vector(int size, int incr);;

HTThanh @ FIT-­HCMUS 26

Page 27: Seminar01 Generic Collections

Class Vector

HTThanh @ FIT-­HCMUS 27

Page 28: Seminar01 Generic Collections

Class Stack

£ Kế thừa class Vector£ Hoạt động theo cơ chế LIFO£ Một số method thông dụng:

p boolean empty() ;;p Object peek( );;p Object pop( );;p Object push(Object element);;

HTThanh @ FIT-­HCMUS 28

Page 29: Seminar01 Generic Collections

Class Stack

HTThanh @ FIT-­HCMUS 29

Page 30: Seminar01 Generic Collections

Class HashSet

£ Cài đặt interface Set

HTThanh @ FIT-­HCMUS 30

Page 31: Seminar01 Generic Collections

Class TreeSet

£ Cài đặt SortedSet

HTThanh @ FIT-­HCMUS 31

Page 32: Seminar01 Generic Collections

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 + ")";;

Page 33: Seminar01 Generic Collections

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

Page 34: Seminar01 Generic Collections

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

Page 35: Seminar01 Generic Collections

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

Page 36: Seminar01 Generic Collections

Class HashMap

£ Cài đặt interface Map

HTThanh @ FIT-­HCMUS 36

Page 37: Seminar01 Generic Collections

Class TreeMap

£ Cài đặt SortedMap

HTThanh @ FIT-­HCMUS 37

Page 38: Seminar01 Generic Collections

Algorithm

HTThanh @ FIT-­HCMUS 38

Page 39: Seminar01 Generic Collections

Algorithm

HTThanh @ FIT-­HCMUS 39

Page 40: Seminar01 Generic Collections

JAVA GENERICSHồ Tuấn Thanh

HTThanh @ FIT-­HCMUS 40

Page 41: Seminar01 Generic Collections

Đặt vấn đề

HTThanh @ FIT-­HCMUS 41

Page 42: Seminar01 Generic Collections

Khắc phục

HTThanh @ FIT-­HCMUS 42

Page 43: Seminar01 Generic Collections

Lời khuyên

HTThanh @ FIT-­HCMUS 43

Page 44: Seminar01 Generic Collections

Generic Class

HTThanh @ FIT-­HCMUS 44

Page 45: Seminar01 Generic Collections

Generic Function

HTThanh @ FIT-­HCMUS 45

Output

Page 46: Seminar01 Generic Collections

Wildcards

HTThanh @ FIT-­HCMUS 46

“?”

“? extends type”

“? super type”

Page 47: Seminar01 Generic Collections

Tài liệu tham khảo

£ Slide “Java Collections” – Professor Evan Korth

£ Slide “Java Generics & Collections” – thầy Nguyễn Lê Hoàng Dũng

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

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

HTThanh @ FIT-­HCMUS 47

Page 48: Seminar01 Generic Collections

BÀI TẬPHồ Tuấn Thanh

48HTThanh @ FIT-­‐HCMUS

Page 49: Seminar01 Generic Collections

Bài tập

£ Bài tập cá nhân£ Viết chương trình ứng dụng WinForm1. Hiển thị danh sách sinh viên2. Tìm sinh viên theo ít nhất 2 tiêu chí (ngườidùng có thể nhập vào 1 hoặc 2 tiêu chí)

3. Thêm mới sinh viên4. Cập nhật thông tin sinh viên5. Xóa thông tin sinh viên

49HTThanh @ FIT-­HCMUS

Page 50: Seminar01 Generic Collections

Bài tập

£ Yêu cầu kĩ thuậtp Javap Lưu trữ plain text filep Swingp Generic, collections

£ Bài nộp: MSSV.rar/zipp Checklist.txt: tự chấm điểm cho 5 chức năng ở trên, mỗi chức năng thang 10

p Thư mục Source code: chứa toàn bộ mã nguồnp Thư mục Data: chứa file dữ liệu, sinhvien.txtp Thư mục Demo: hình ảnh demo các chức năng, mỗichức năng có thể chụp >=1 hình, đặt STT (1-­5) đúng

p Thư mục EXE: chứa file jar chạy được

50HTThanh @ FIT-­HCMUS

Page 51: Seminar01 Generic Collections

HTThanh @ FIT-­HCMUS 51