Bài giảng Cấu trúc dữ liệu: Chương 4 - Nguyễn Xuân Vinh
Số trang: 35
Loại file: pptx
Dung lượng: 205.67 KB
Lượt xem: 11
Lượt tải: 0
Xem trước 4 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng Cấu trúc dữ liệu - Chương 4: Iterator - Comparable - Comparator trình bày về java conllection architecture, examining sets and maps, iterator methods, iterator example, comparable, comparator,...Mời bạn đọc cùng tham khảo.
Nội dung trích xuất từ tài liệu:
Bài giảng Cấu trúc dữ liệu: Chương 4 - Nguyễn Xuân VinhGV: NGUYỄN XUÂN VINH CẤU TRÚC DỮ LIỆU DATA STRUCTURES [214331] Iterator - Comparable - ComparatorMÔN: CẤU TRÚC DỮ LIỆU Nguyễn Xuân Vinh6/12/14 nguyenxuanvinh@hcmuaf.ed u.vn/XX12 /XX 6/12/14 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Java Collection Architecture ListGV: NGUYỄN XUÂN VINH The Differences!!! index 0 1 2 3 4 5 6 7 8 9 • How to browse element? value 3 8 9 7 5 12 0 0 0 0 – Elements of List are indexed. LinkedList intvalue=list[0]; – Elements of LinkedList, Sets andMÔN: CẤU TRÚC DỮ LIỆU Maps cant be accessed by the index Set to we • How to remove element? from Map6/12/14/XX3GV: NGUYỄN XUÂN VINH Examining sets and maps • elements of Java Sets and Maps cant be accessed by index – must use a foreach loop: Setscores=newHashSet(); for(intscore:scores){MÔN: CẤU TRÚC DỮ LIỆU System.out.println(Thescoreis+score); } – Problem: foreach is read-only; cannot modify set while looping for(intscore:scores){ if(scoreGV: NGUYỄN XUÂN VINH Iterators (11.1) • iterator: An object that allows a client to traverse the elements of any collection, regardless of its implementation. – Remembers a position within a collection, and allows you to: • get the element at that position • advance to the next positionMÔN: CẤU TRÚC DỮ LIỆU • (possibly) remove or change the element at that position – Benefit: A common way to examine any collections elements. list inde 0 1 2 3 4 5 6 7 8 9 the set x to we from6/12/14 valu 3 8 9 7 5 12 0 0 0 0 e currentelement: 9 currentelement: from/XX iterator iterator size currentindex: 6 2 nextelement:the5GV: NGUYỄN XUÂN VINH Iterator methods hasNext() returnstrueiftherearemoreelementstoexamine next() returnsthenextelementfromthecollection(throwsa NoSuchElementExceptioniftherearenoneleftto examine)MÔN: CẤU TRÚC DỮ LIỆU remove() removesfromthecollectionthelastvaluereturnedby next()(throwsIllegalStateExceptionifyouhave notcallednext()yet) • Iterator interface in java.util – every collection has an iterator() method that retur ...
Nội dung trích xuất từ tài liệu:
Bài giảng Cấu trúc dữ liệu: Chương 4 - Nguyễn Xuân VinhGV: NGUYỄN XUÂN VINH CẤU TRÚC DỮ LIỆU DATA STRUCTURES [214331] Iterator - Comparable - ComparatorMÔN: CẤU TRÚC DỮ LIỆU Nguyễn Xuân Vinh6/12/14 nguyenxuanvinh@hcmuaf.ed u.vn/XX12 /XX 6/12/14 MÔN: CẤU TRÚC DỮ LIỆU GV: NGUYỄN XUÂN VINH Java Collection Architecture ListGV: NGUYỄN XUÂN VINH The Differences!!! index 0 1 2 3 4 5 6 7 8 9 • How to browse element? value 3 8 9 7 5 12 0 0 0 0 – Elements of List are indexed. LinkedList intvalue=list[0]; – Elements of LinkedList, Sets andMÔN: CẤU TRÚC DỮ LIỆU Maps cant be accessed by the index Set to we • How to remove element? from Map6/12/14/XX3GV: NGUYỄN XUÂN VINH Examining sets and maps • elements of Java Sets and Maps cant be accessed by index – must use a foreach loop: Setscores=newHashSet(); for(intscore:scores){MÔN: CẤU TRÚC DỮ LIỆU System.out.println(Thescoreis+score); } – Problem: foreach is read-only; cannot modify set while looping for(intscore:scores){ if(scoreGV: NGUYỄN XUÂN VINH Iterators (11.1) • iterator: An object that allows a client to traverse the elements of any collection, regardless of its implementation. – Remembers a position within a collection, and allows you to: • get the element at that position • advance to the next positionMÔN: CẤU TRÚC DỮ LIỆU • (possibly) remove or change the element at that position – Benefit: A common way to examine any collections elements. list inde 0 1 2 3 4 5 6 7 8 9 the set x to we from6/12/14 valu 3 8 9 7 5 12 0 0 0 0 e currentelement: 9 currentelement: from/XX iterator iterator size currentindex: 6 2 nextelement:the5GV: NGUYỄN XUÂN VINH Iterator methods hasNext() returnstrueiftherearemoreelementstoexamine next() returnsthenextelementfromthecollection(throwsa NoSuchElementExceptioniftherearenoneleftto examine)MÔN: CẤU TRÚC DỮ LIỆU remove() removesfromthecollectionthelastvaluereturnedby next()(throwsIllegalStateExceptionifyouhave notcallednext()yet) • Iterator interface in java.util – every collection has an iterator() method that retur ...
Tìm kiếm theo từ khóa liên quan:
Cấu trúc dữ liệu Bài giảng cấu trúc dữ liệu Cấu trúc dữ liệu Chương 4 Java conllection architecture Examining sets and maps Iterator comparable comparatorTài liệu liên quan:
-
Đề cương chi tiết học phần Cấu trúc dữ liệu và giải thuật (Data structures and algorithms)
10 trang 324 0 0 -
Giải thuật và cấu trúc dữ liệu
305 trang 165 0 0 -
Bài giảng Phân tích thiết kế phần mềm: Chương 1 - Trường ĐH Ngoại ngữ - Tin học TP.HCM
64 trang 155 0 0 -
Tập bài giảng Thực hành kỹ thuật lập trình
303 trang 143 0 0 -
Giáo trình Cấu trúc dữ liệu và thuật toán (Tái bản): Phần 1
152 trang 140 0 0 -
Tài liệu tham khảo: Cấu trúc dữ liệu và giải thuật
229 trang 128 0 0 -
Lập trình C - Cấu trúc dữ Liệu
307 trang 77 0 0 -
Ứng dụng và cài đặt cấu trúc dữ liệu bằng C: Phần 1
338 trang 76 0 0 -
Bài giảng Cấu trúc dữ liệu và thuật toán: Chương 3 - Một số mô hình thuật toán
42 trang 75 0 0 -
49 trang 74 0 0