20090211-ADT-II

20090211-ADT-II - We will not look at all the required...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Implementation options • There are two basic approaches: – array-based – linked • We will consider array-based implementation first • Use a TDD approach: first consider what the essential functionality of a given ADT is.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays • An array is a collection of variables, all of the same type. • An array is created with the new operator. • The size of an array must be specified at creation time. • The memory for an array is allocated in a contiguous block. • The size of an array is fixed.
Background image of page 2
Array indexing • A variable in an array is accessed using special syntax. – Suppose Object[] x = new Object[4]; – Variables: x[0], x[1], x[2] and x[3] – Index ranges from 0 to size-1 .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Generics • You’ve seen how to use generic collections in CSE115. • We’ll discuss later how to define them – don’t sweat these details now.
Background image of page 4
Collection interface • We’ll set up our Bag implementation to implement the java.util.Collection interface. • Method signatures come from this interface.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: We will not look at all the required methods today, only the four identified earlier: add, remove, contains and size public class Bag<E> implements Collection<E> { private final static int DEFAULT_CAPACITY = 10; private E _store; private int _size; public Bag() { this(DEFAULT_CAPACITY); } public Bag(int initialCapacity) { if (initialCapacity < 1) { initialCapacity = DEFAULT_CAPACITY; } _store = (E) new Object[initialCapacity]; _size = 0; } public boolean add(E obj) { if (_size == _store.length) { resize(); } _store[_size] = obj; _size++; return true; } public boolean remove(Object obj) { for (int i = 0; i < _size; i++) { if (_store[i] == obj) { _size--; _store[i] = _store[_size]; _store[_size] = null; return true; } } return false; } public boolean contains(Object obj) { for (int i = 0; i < _size; i++) { if (_store[i] == obj) { return true; } } return false; } public int size() { return _size; }...
View Full Document

Page1 / 10

20090211-ADT-II - We will not look at all the required...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online