Tải bản đầy đủ (.pptx) (17 trang)

Common Data Structures

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (796.21 KB, 17 trang )

Common Data Structures
Mini Report
Hien Vu
4/15/2014
ArrayList
Linked List
Hash Map
Hash Set
1

ArrayLi
st
Remind: Array
1

ArrayLi
st
Remind: Array
Ex: int[] number = new int[100];
=> fixed-size array declaration
Problems: ???
1

ArrayLi
st
Array Lists: offer two significant
benefits:

Array lists can grow and shrink as
needed.


The ArrayList class supplies
methods for many common tasks,
such as inserting and removing
elements.
1

ArrayLi
st
Syntax:
1

ArrayLi
st
Figure 1.1 Adding an Element with add
1

ArrayLi
st
Figure 1.2 Adding and Removing Elements in the Middle of an Array List
1

ArrayLi
st
SN Methods with Description
1
void add(int index, Object element)
Inserts the specified element at the specified position index in this list.
2
boolean add(Object o)
Appends the specified element to the end of this list.

3
boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list, starting at the specified
position.
4
boolean contains(Object o)
Returns true if this list contains the specified element. More formally, returns true if and
only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
5
Object get(int index)
Returns the element at the specified position in this list.
6
Object remove(int index)
Removes the element at the specified position in this list.
7
int size()
Returns the number of elements in this list.
2

Linked
List
A linked list is a data structure used
for collecting a sequence of objects
that allows efficient addition and
removal of elements in the middle of
the sequence.
2

Linked
List

Figure 2.1 Inserting an Element into a Linked List
A linked list consists of a number of nodes,
each of which has a reference to the next
node.
2

Linked
List

Adding and removing elements in
the middle of a linked list is efficient.

Visiting the elements of a linked list
in sequential order is efficient, but
random access is not.

You use a list iterator to access
elements inside a linked list.
2

Linked
List
Linked List Methods:
Mothods
boolean
add(E e) Appends the specified element to the end of this list.
void
add(int index, E element) Inserts the specified element at the specified
position in this list.
void

addFirst(E e) Inserts the specified element at the beginning of this list.
void
addLast(E e) Appends the specified element to the end of this list.
void
clear() Removes all of the elements from this list.
E
element() Retrieves, but does not remove, the head (first element) of this
list.
E
get(int index) Returns the element at the specified position in this list.
E
getFirst() Returns the first element in this list.
E
getLast() Returns the last element in this list.
2

Hash
Set
Set & Hash Set

A set is an unordered collection of
distinct elements. Elements can be
added, located, and removed.

Sets don’t have duplicates. Adding
a duplicate of an element that is
already present is silently ignored.
2

Hash

Set
Set & Hash Set
Set Classes and Interfaces in the Standard Library
2

Hash
Set
Set & Hash Set

Ex:
Set<String> names = new HashSet<String>();

Adding and removing set elements is
straightforward:
names.add("Romeo");
names.remove("Juliet");

The containsmethod tests whether an element is
contained in the set:
if (names.contains("Juliet")) . . .

To visit all elements in a set, use an iterator.
2

Hash
Set
Set & Hash Set

To visit all elements in a set, use an iterator.
Iterator<String> iter = names.iterator();

while (iter.hasNext()){
String name = iter.next();
Do something withname
}
OR
for (String name : names){
//Do something withname
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×