Package com.zybooks.dsaj.list
Class ArrayList<E>
java.lang.Object
com.zybooks.dsaj.list.ArrayList<E>
- Type Parameters:
E
- the element type
Realization of a list by means of a dynamic array. This is a simplified version
of the java.util.ArrayList class.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
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].get
(int i) Returns (but does not remove) the element at index i.boolean
isEmpty()
Tests whether the array list is empty.iterator()
Returns an iterator of the elements stored in the list.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 the internal array to have given capacity >= size.Replaces the element at the specified index, and returns the element previously stored.int
size()
Returns the number of elements in the list.toString()
Produces a string representation of the contents of the indexed list.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
CAPACITY
public static final int CAPACITYDefault array capacity.- See Also:
-
-
Constructor Details
-
ArrayList
public ArrayList()Creates an array list with default initial capacity. -
ArrayList
public ArrayList(int capacity) Creates an array list with given initial capacity.- Parameters:
capacity
- the initial capacity of the dynamic array
-
-
Method Details
-
size
public int size()Returns the number of elements in the list. -
isEmpty
public boolean isEmpty()Tests whether the array list is empty. -
get
Returns (but does not remove) the element at index i.- Specified by:
get
in interfaceList<E>
- Parameters:
i
- the index of the element to return- Returns:
- the element at the specified index
- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()-1
-
set
Replaces the element at the specified index, and returns the element previously stored.- Specified by:
set
in interfaceList<E>
- Parameters:
i
- the index of the element to replacee
- the new element to be stored- Returns:
- the previously stored element
- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()-1
-
add
Inserts the given element at the specified index of the list, shifting all subsequent elements in the list one position further to make room.- Specified by:
add
in interfaceList<E>
- Parameters:
i
- the index at which the new element should be storede
- the new element to be stored- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()
-
remove
Removes and returns the element at the given index, shifting all subsequent elements in the list one position closer to the front.- Specified by:
remove
in interfaceList<E>
- Parameters:
i
- the index of the element to be removed- Returns:
- the element that had be stored at the given index
- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()
-
checkIndex
Checks whether the given index is in the range [0, n-1].- Parameters:
i
- the given indexn
- one greater than the maximum allowable index- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than n
-
resize
protected void resize(int capacity) Resizes the internal array to have given capacity >= size.- Parameters:
capacity
- the new capacity
-
iterator
Returns an iterator of the elements stored in the list. -
toString
Produces a string representation of the contents of the indexed list. This exists for debugging purposes only.
-