Array Lists

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/63

encourage image

There's no tags or description

Looks like no tags are added yet.

64 Terms

1
New cards

What is a List ADT?

A list abstract data type represents a linear sequence of elements with operations to add, remove, and access elements.

2
New cards

What is an index?

An integer that identifies an element's position in a list.

3
New cards

What is the main advantage of array indexing?

Elements can be accessed directly using their index in O(1) time.

4
New cards

Why is accessing an indexed linked-list element slower?

You must traverse the list from node to node to reach the desired index.

5
New cards

What does size() return?

The number of elements stored in the list.

6
New cards

What does isEmpty() return?

A boolean indicating whether the list contains zero elements.

7
New cards

What does get(i) do?

Returns the element at index i.

8
New cards

What does set(i, e) do?

Replaces the element at index i with e and returns the old element.

9
New cards

What does add(i, e) do?

Inserts e at index i and shifts later elements one position right.

10
New cards

What does remove(i) do?

Removes and returns the element at index i, shifting later elements left.

11
New cards

What happens when a list method receives an invalid index?

An IndexOutOfBoundsException may be thrown.

12
New cards

What is an ArrayList implementation?

A list implementation that stores element references in an underlying array.

13
New cards

How does an array list implement get(i)?

It accesses the array directly at A[i].

14
New cards

How does an array list implement set(i, e)?

It replaces the reference stored at A[i] with e.

15
New cards

Why can add(i, e) be expensive?

Elements after index i may need to shift one position right.

16
New cards

Why can remove(i) be expensive?

Elements after index i may need to shift one position left.

17
New cards

What is the space complexity of an array-based list?

O(n), where n is the number of stored elements.

18
New cards

What is the time complexity of get(i)?

O(1), constant time.

19
New cards

What is the time complexity of set(i, e)?

O(1), constant time.

20
New cards

What is the time complexity of add(i, e)?

O(n) in the worst case because elements may need shifting.

21
New cards

What is the time complexity of remove(i)?

O(n) in the worst case because elements may need shifting.

22
New cards

What is a limitation of a fixed-capacity array list?

It cannot store more elements than its allocated capacity.

23
New cards

What happens when a fixed-capacity array is full?

Adding another element may cause an exception.

24
New cards

Why can choosing an array that is too large waste memory?

Unused array positions still occupy allocated memory.

25
New cards

Why can choosing an array that is too small be a problem?

The array may run out of capacity before all elements fit.

26
New cards

What is a dynamic array?

An array-backed list that can grow when its current capacity is full.

27
New cards

How does a dynamic array store its elements?

In an internal array that may have more capacity than the current list size.

28
New cards

What happens when a dynamic array runs out of capacity?

It allocates a larger array and copies the existing element references.

29
New cards

What are the four steps for growing a dynamic array?

Allocate a larger array, copy elements, switch to the new array, and insert the new element.

30
New cards

Why does a dynamic array allocate extra capacity?

To allow future insertions without replacing the array every time.

31
New cards

What is push in the slides?

Adding an element to the end of the dynamic array.

32
New cards

What happens during a dynamic array resize?

A larger array is allocated and the existing element references are copied.

33
New cards

Why can resizing a dynamic array be expensive?

Copying n existing elements takes O(n) time.

34
New cards

What is amortized analysis?

An analysis of the average cost per operation over a sequence of operations.

35
New cards

Why is doubling array capacity efficient?

One expensive resize allows many subsequent insertions without resizing.

36
New cards

What is the amortized time complexity of adding to the end of a dynamic array?

O(1) amortized time.

37
New cards

What is the worst-case time complexity of an individual push during resizing?

O(n), because existing elements must be copied.

38
New cards

Why is doubling capacity better than growing by one slot?

It reduces how frequently expensive resizing and copying are needed.

39
New cards

What is the difference between worst-case and amortized time?

Worst-case considers one operation; amortized time averages costs over a sequence.

40
New cards

Why can StringBuilder be efficient for building long strings?

It uses a growable storage strategy, reducing repeated expensive allocations and copying.

41
New cards
If an array list has 5 elements, what are its valid element indexes?
0, 1, 2, 3, and 4.
42
New cards
If a list has size 5, is add(5, e) valid?
Yes. Insertion is allowed at index size, which appends to the end.
43
New cards
If a list has size 5, is get(5) valid?
No. Valid element indexes are 0 through 4.
44
New cards
If you insert at index 1, which elements shift?
All elements originally at index 1 and later shift one position right.
45
New cards
If you remove the element at index 2, which elements shift?
All elements originally after index 2 shift one position left.
46
New cards
If an array list contains [A, B, C], what does add(1, X) produce?
[A, X, B, C]
47
New cards
If an array list contains [A, B, C], what does remove(1) leave?
[A, C]. The removed element is B.
48
New cards
If an array list contains [A, B, C], what does set(1, X) produce?
[A, X, C]. The old element B is returned.
49
New cards
Why is get(i) faster in an array than in a linked list?
An array calculates the location directly instead of traversing nodes.
50
New cards
A dynamic array has capacity 8 and size 8. What happens when you add another item?
It allocates a larger array, copies the 8 references, then inserts the new item.
51
New cards
Why are not all dynamic-array insertions equally expensive?
Most insertions use free capacity; occasional insertions trigger an expensive resize.
52
New cards
Does O(1) amortized mean every insertion takes constant time?
No. Some insertions take O(n), but the average over many insertions is O(1).
53
New cards
If an array list has 5 elements, what are its valid element indexes?
0, 1, 2, 3, and 4.
54
New cards
If a list has size 5, is add(5, e) valid?
Yes. Insertion is allowed at index size, which appends to the end.
55
New cards
If a list has size 5, is get(5) valid?
No. Valid element indexes are 0 through 4.
56
New cards
If you insert at index 1, which elements shift?
All elements originally at index 1 and later shift one position right.
57
New cards
If you remove the element at index 2, which elements shift?
All elements originally after index 2 shift one position left.
58
New cards
If an array list contains [A, B, C], what does add(1, X) produce?
[A, X, B, C]
59
New cards
If an array list contains [A, B, C], what does remove(1) leave?
[A, C]. The removed element is B.
60
New cards
If an array list contains [A, B, C], what does set(1, X) produce?
[A, X, C]. The old element B is returned.
61
New cards
Why is get(i) faster in an array than in a linked list?
An array calculates the location directly instead of traversing nodes.
62
New cards
A dynamic array has capacity 8 and size 8. What happens when you add another item?
It allocates a larger array, copies the 8 references, then inserts the new item.
63
New cards
Why are not all dynamic-array insertions equally expensive?
Most insertions use free capacity; occasional insertions trigger an expensive resize.
64
New cards
Does O(1) amortized mean every insertion takes constant time?
No. Some insertions take O(n), but the average over many insertions is O(1).