ArrayList - class

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/11

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

12 Terms

1
New cards

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

2
New cards

int indexOf(Object o)

  • Iterate through array—stop at last index (size - 1)

  • If current index value equals object

    • Return index

  • Otherwise—return -1

3
New cards

E get(int index)

  • If the index is inbounds

    • Return value at that index

4
New cards

boolean add(E e)

  • Ensure the capacity can add 1 more—size + 1

  • Put e at the next open spot—size

  • Increment size

5
New cards

E set(int index, E element)

  • Make sure index is inbounds

  • Hold former

  • Set index to new element

  • Return former

6
New cards

Object[] toArray()

  • Create new array with capacity of size

  • iterate through current array—put each element in new array

  • Return new array

7
New cards

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

8
New cards

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

9
New cards

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

10
New cards

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()

11
New cards

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

12
New cards

ArrayListIterator — inner class

  • hasNext()

    • Make sure nextIndex is a valid index

  • next()

    • Hold element at nextIndex

    • Check if its null

    • Advance nextIndex

    • Return value