Arraylist Flashcards

1
Q

What is the main difference between the Array and the Arraylist?

A

An Arraylist’s size is dynamic. It can be thought of as a resizeable array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given that Arraylists have the same BigO score as Arrays, why not always use Arraylists instead of Arrays? (2)

A
  1. If you need to store primitive (auto-boxing generally solves this though).
  2. If you need to minimise memory and processing upkeep.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What data structure in an Arraylist backed by? I.e. What is the Arraylist code using under-the-hood?

A

An Array(s).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 6 common ArrayList methods? (Every language has it’s own implementation but these 6 are used ubiquitous)

A
  1. Add
  2. Remove
  3. Get
  4. Set
  5. Clear
  6. toArray
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the add(Object) method do?

A

It appends the element you pass in to the end of the arraylist.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the add(Object, Index) method do?

A

It appends the element you pass in at the index you pass in. Essentially, this is an INSERT.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the remove(Index) method do?

A

Remove the Object at the index you provide.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the remove(Object) method do?

A

Removes the first instance of the object passed into the arrayList.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the BigO of ACCESSING an element in an Arraylist?

A

O(1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the BigO of SEARCHING for an element in an Arraylist?

A

O(n) - Must perform Linear Search.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the BigO of INSERTING an element into an Arraylist?

A

O(n) - Worst-case: Every element needs to be shifted to the right by one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the BigO of DELETING an element from an Arraylist?

A

O(n) - Worst-case: Every element needs to be shifted to the left by one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does an Arraylist achieve O(1) for accessing data when it is not stored in contiguous blocks in memory?

A

Instead of storing the actual objects which are contained within itself, an Arraylist actually stores references (or pointers) to the locations of those objects in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When should you an Arraylist instead of an Array?

A

In more interactive programs where you’ll be modifying the data quite a bit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Arraylist definition…

A

A dynamically increasing array which comes with a slew of methods to help work it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Is an Arraylist Random or Sequential Access?

A

Random.