Amazon Interview Question

Why would you use ArrayList? Is there a way to implement the same logic using just an ordinary array of int elements? What is the pros and cons in your decision?

Interview Answers

Anonymous

Aug 6, 2013

Arrays don't have methods, ArrayList is a Class. Instances of ArrayList can contain all kinds of objects (depending on how they are declared). They have a good number of public methods for adding/retrieving/removing elements, and are generally a lot more powerful than arrays. In particular they re-size automatically to fit whatever number of elements you put in them, and "close up the gap" when you remove elements from the middle. In other side Array is faster than ArrayList.

Anonymous

Jun 14, 2014

The difference from ArrayList and Array is that in the first you can insert a new object in O(1), instead in an Array, you have to create a new Array and copy all the object plus insert this new. So the operation cost O(n). But Array has better performance to order and to have a direct access to an object. So, for a queue FIFO or LIFO or any other, you ned just to POP and PUSH, and you touch just the first and the last element of the queue..so it is better use an ArrayList. For a list of the faster car or the more expensive car, would be better an array. To implement an ArrayList with an Array, you have just to create for each object in the array list, an array with length 2, where A[0] contains the value, and A[1] contains the address to the next Array object.