Java Reference
In-Depth Information
(a) Array list animation
(b) Linked list animation
F IGURE 24.1
The animation tool enables you to see how array lists and linked lists work.
MyArrayList
java.lang.Iterable
MyList
MyAbstractList
MyLinkedList
F IGURE 24.2
MyList defines a common interface for MyAbstractList , MyArrayList ,
and MyLinkedList .
L ISTING 24.1
MyList.java
1 public interface MyList<E> extends java.lang.Iterable<E> {
2
/** Add a new element at the end of this list */
3
public void add(E e);
add(e)
4
5
/** Add a new element at the specified index in this list */
6
public void add( int index, E e);
add(index, e)
7
8
/** Clear the list */
9
public void clear();
clear()
10
11
/** Return true if this list contains the element */
12
public boolean contains(E e);
contains(e)
13
14
/** Return the element from this list at the specified index */
15
public E get( int index);
get(index)
16
17 /** Return the index of the first matching element in this list.
18 * Return -1 if no match. */
19
public int indexOf(E e);
indexOf(e)
20
21
/** Return true if this list doesn't contain any elements */
22
public boolean isEmpty();
isEmpty(e)
23
24 /** Return the index of the last matching element in this list
25 * Return -1 if no match. */
26
public int lastIndexOf(E e);
lastIndexOf(e)
27
28 /** Remove the first occurrence of the element e from this list.
29 * Shift any subsequent elements to the left.
30 * Return true if the element is removed. */
 
 
Search WWH ::




Custom Search