Danh mục

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    
Thư viện của tui

Hỗ trợ phí lưu trữ khi tải xuống: 11,000 VND Tải xuống file đầy đủ (35 trang) 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 ...

Tài liệu được xem nhiều: