Arraylist Flashcards
What is the main difference between the Array and the Arraylist?
An Arraylist’s size is dynamic. It can be thought of as a resizeable array.
Given that Arraylists have the same BigO score as Arrays, why not always use Arraylists instead of Arrays? (2)
- If you need to store primitive (auto-boxing generally solves this though).
- If you need to minimise memory and processing upkeep.
What data structure in an Arraylist backed by? I.e. What is the Arraylist code using under-the-hood?
An Array(s).
What are the 6 common ArrayList methods? (Every language has it’s own implementation but these 6 are used ubiquitous)
- Add
- Remove
- Get
- Set
- Clear
- toArray
What does the add(Object) method do?
It appends the element you pass in to the end of the arraylist.
What does the add(Object, Index) method do?
It appends the element you pass in at the index you pass in. Essentially, this is an INSERT.
What does the remove(Index) method do?
Remove the Object at the index you provide.
What does the remove(Object) method do?
Removes the first instance of the object passed into the arrayList.
What is the BigO of ACCESSING an element in an Arraylist?
O(1)
What is the BigO of SEARCHING for an element in an Arraylist?
O(n) - Must perform Linear Search.
What is the BigO of INSERTING an element into an Arraylist?
O(n) - Worst-case: Every element needs to be shifted to the right by one.
What is the BigO of DELETING an element from an Arraylist?
O(n) - Worst-case: Every element needs to be shifted to the left by one.
How does an Arraylist achieve O(1) for accessing data when it is not stored in contiguous blocks in memory?
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.
When should you an Arraylist instead of an Array?
In more interactive programs where you’ll be modifying the data quite a bit.
Arraylist definition…
A dynamically increasing array which comes with a slew of methods to help work it.