Modifier and Type | Field and Description |
---|---|
static int |
CAPACITY
Default array capacity.
|
Constructor and Description |
---|
ArrayList()
Creates an array list with default initial capacity.
|
ArrayList(int capacity)
Creates an array list with given initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int i,
E e)
Inserts the given element at the specified index of the list, shifting all
subsequent elements in the list one position further to make room.
|
protected void |
checkIndex(int i,
int n)
Checks whether the given index is in the range [0, n-1].
|
E |
get(int i)
Returns (but does not remove) the element at index i.
|
boolean |
isEmpty()
Tests whether the array list is empty.
|
Iterator<E> |
iterator()
Returns an iterator of the elements stored in the list.
|
static void |
main(String[] args) |
E |
remove(int i)
Removes and returns the element at the given index, shifting all subsequent
elements in the list one position closer to the front.
|
protected void |
resize(int capacity)
Resizes internal array to have given capacity >= size.
|
E |
set(int i,
E e)
Replaces the element at the specified index, and returns the element previously stored.
|
int |
size()
Returns the number of elements in the list.
|
String |
toString()
Produces a string representation of the contents of the indexed list.
|
public static final int CAPACITY
public ArrayList()
public ArrayList(int capacity)
public int size()
public boolean isEmpty()
public E get(int i) throws IndexOutOfBoundsException
get
in interface List<E>
i
- the index of the element to returnIndexOutOfBoundsException
- if the index is negative or greater than size()-1public E set(int i, E e) throws IndexOutOfBoundsException
set
in interface List<E>
i
- the index of the element to replacee
- the new element to be storedIndexOutOfBoundsException
- if the index is negative or greater than size()-1public void add(int i, E e) throws IndexOutOfBoundsException
add
in interface List<E>
i
- the index at which the new element should be storede
- the new element to be storedIndexOutOfBoundsException
- if the index is negative or greater than size()public E remove(int i) throws IndexOutOfBoundsException
remove
in interface List<E>
i
- the index of the element to be removedIndexOutOfBoundsException
- if the index is negative or greater than size()protected void checkIndex(int i, int n) throws IndexOutOfBoundsException
IndexOutOfBoundsException
protected void resize(int capacity)
public String toString()
public static void main(String[] args)