E
- the type of elements in this listIArrayList<E>
, IList<E>
public class MyArrayList<E> extends java.lang.Object implements IArrayList<E>
Modifier and Type | Field | Description |
---|---|---|
static double |
CAPACITY_INCREASE |
Constructor | Description |
---|---|
MyArrayList() |
Constructs an empty list with an initial capacity of ten.
|
MyArrayList(int initialCapacity) |
Constructs an empty list with the specified initial capacity.
|
Modifier and Type | Method | Description |
---|---|---|
void |
add(int index,
E e) |
Inserts the specified element at the specified position in this list.
|
boolean |
add(E e) |
Appends the specified element to the end of this list.
|
void |
clear() |
Removes all of the elements from this list.
|
boolean |
contains(java.lang.Object o) |
Returns true if this list contains the specified element.
|
E |
get(int index) |
Returns the element at the specified position in this list.
|
int |
indexOf(java.lang.Object o) |
Returns the index of the first occurrence of the specified element in
this list, or -1 if this list does not contain the element.
|
boolean |
isEmpty() |
|
int |
lastIndexOf(java.lang.Object o) |
Returns the index of the last occurrence of the specified element in this list, or -1 if
this list does not contain the element.
|
void |
printDebug() |
Special debug method
|
E |
remove(int index) |
Removes the element at the specified position in this list.
|
boolean |
remove(java.lang.Object o) |
Removes the first occurrence of the specified element from this list, if it is present.
|
void |
removeRange(int fromIndex,
int toIndex) |
Removes from this list all of the elements whose index is between fromIndex,
inclusive, and toIndex, exclusive.
|
E |
set(int index,
E e) |
|
int |
size() |
|
void |
trimToSize() |
Trims the capacity to the size of the existing array
|
public static final double CAPACITY_INCREASE
public MyArrayList(int initialCapacity)
initialCapacity
- the initial capacity of the listpublic MyArrayList()
Think about constructor chaining.
public void printDebug()
public boolean add(E e)
IList
public void add(int index, E e)
IList
public boolean remove(java.lang.Object o)
IList
public E remove(int index)
IList
public void removeRange(int fromIndex, int toIndex)
IList
(toIndex - fromIndex)
elements.
(If toIndex==fromIndex
, this operation has no effect.)removeRange
in interface IList<E>
fromIndex
- index of first element to be removedtoIndex
- index after last element to be removedpublic E get(int index)
IList
public boolean contains(java.lang.Object o)
IList
(o==null ? e==null : o.equals(e))
.public int indexOf(java.lang.Object o)
IList
(o==null ? get(i)==null : o.equals(get(i)))
, or -1
if there is no such index.public int lastIndexOf(java.lang.Object o)
IList
(o==null ? get(i)==null : o.equals(get(i)))
, or -1
if there is no such index.lastIndexOf
in interface IList<E>
o
- element to search forpublic void clear()
IList
public int size()
public boolean isEmpty()
public void trimToSize()
IArrayList
trimToSize
in interface IArrayList<E>