1/11
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
boolean contains(Object o)
Iterate through array—stop at last index (size - 1)
Check if current index value is equal to object
Return true
Otherwise—return false
int indexOf(Object o)
Iterate through array—stop at last index (size - 1)
If current index value equals object
Return index
Otherwise—return -1
E get(int index)
If the index is inbounds
Return value at that index
boolean add(E e)
Ensure the capacity can add 1 more—size + 1
Put e at the next open spot—size
Increment size
E set(int index, E element)
Make sure index is inbounds
Hold former
Set index to new element
Return former
Object[] toArray()
Create new array with capacity of size
iterate through current array—put each element in new array
Return new array
boolean remove(Object o) — KEY
Invoke indexOf() to get index of object
Return false if -1
Iterate from that index—to 1 before the last element
Each iteration will shift each element to the left
Delete the duplicate of the final element—size - 1
Decrement size
E remove(int index) — KEY
Store the value that we are removing—at index
Iterate from the index—to 1 before the last element
Each iteration will shift each element to the left
Delete the duplicate of the final element—size - 1
Decrement size
void add(int index, E element) — KEY
Make sure the index is bounds
Ensure the capacity can add 1 more element— index + 1
Reverse for loop
Start at 1 after last element
Shift all elements to the right—stopping at 1 before index
Assign “element” to the duplicate at index
Increment size
boolean addAll(Collection<E> c)
Ensure the capacity can hold size + the size of the collection
Iterator on collection—send over each element to add()
void ensureCapacity(int minCapacity)
Check if capacity is less than min
Create new capacity
Double check—new capacity greater than min
Create new array with new capacity
Iterate through each element—add to new array
Overwrite elementData with new array
ArrayListIterator — inner class
hasNext()
Make sure nextIndex is a valid index
next()
Hold element at nextIndex
Check if its null
Advance nextIndex
Return value