Arrays Flashcards
Show me how to initialize a regular array
int[] array = new int[5]
5 being the size
Show me how to initialize a array list
import java.util.ArrayList;
ArrayList<Integer> array = new ArrayList<>();</Integer>
Array Add to End Complexity
O(1)
Iterate through the Array complexity (search)
O(n)
Insert at begining and elsewhere in the array Complexity
O(n)
Delete and access elements at Index x
O(1)
How would you reverse an array?
Have a pointer at the start and end of the array, then loop through incrementing start then decrementing end making sure that they switch variables
While start < end
- temp value stores start
- start value = end value
- end value = temp value
- increment start
- decrement end, repeat
Difference between an array and an arraylist
Arrays have a set size
Arraylists are dynamic