1. Trang chủ
  2. » Luận Văn - Báo Cáo

Bài giảng Công nghệ Java: Chương 13 - PhD. Trần Quang Diệu

10 8 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 427,16 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

• A collection is an object that groups multiple elements into a single unit.. • Very useful.[r]

Trang 1

4/7/2018 http://sites.google.com/site/tranlectures 1

CÔNG NGHỆ JAVA

CH13 JAVA COLLECTIONS

Quang Dieu Tran PhD.

http://sites.google.com/site/tranlectures

Trang 2

Readings and References

• References

– "Collections", Java tutorial

Trang 3

Java 2 Collections

• A collection is an object that groups multiple

elements into a single unit

• Very useful

– store, retrieve and manipulate data

– data structures and methods written by hotshots in the field

• Joshua Bloch, who also wrote the Collections tutorial

Trang 4

Collections Framework

• Unified architecture for representing and

manipulating collections

• A collections framework contains three things

– Interfaces

Trang 5

Collections Framework Diagram

•Interfaces, Implementations, and Algorithms

•From Thinking in Java, page 462

Trang 6

Collection Interface

• Defines fundamental methods

– int size();

– boolean isEmpty();

– boolean contains(Object element);

– boolean add(Object element); // Optional

– boolean remove(Object element); // Optional

– Iterator iterator();

• These methods are enough to define the basic

behavior of a collection

• Provides an Iterator to step through the elements in the Collection

Trang 7

Iterator Interface

• Defines three fundamental methods

– Object next()

– boolean hasNext()

– void remove()

• These three methods provide access to the

contents of the collection

• An Iterator knows position within collection

• Each call to next() “reads” an element from the

collection

– Then you can use it or remove it

Trang 8

Iterator Position

Trang 9

Example - SimpleCollection

public class SimpleCollection {

public static void main(String[] args) {

Collection c;

c = new ArrayList();

System.out.println(c.getClass().getName());

for (int i=1; i <= 10; i++) {

c.add(i + " * " + i + " = "+i*i);

} Iterator iter = c.iterator();

while (iter.hasNext())

System.out.println(iter.next());

}

}

Trang 10

List Interface Context

Collection

List

Ngày đăng: 10/03/2021, 13:58

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w