Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Answer the following questions with reference to the Java ArrayList class: (a

ID: 3842237 • Letter: 1

Question

1. Answer the following questions with reference to the Java ArrayList class:

(a) The underlying data structure of a Java ArrayList object is an array. Explain how each of the following methods of the ArrayList class use and modify the array in order to produce the desired result:

(i) void add(int index, E element) [25%]

(ii) E remove(int index) [10%]

(iii) E set(int index, E element) [10%]

where E in each of the method signatures above is the type parameter of the ArrayList object.

(b) By making reference to the following ArrayList method: E get(int index) Explain when it is more preferable (in terms of computational efficiency) to use an array rather than an ArrayList object for storing data. [20%]

(c) The signature of one of the constructors of the ArrayList class is: ArrayList(int initialCapacity) Explain the purpose of the parameter initialCapacity (i.e., what does the parameter specify, when should it be used and why). [25%]

(d) The API of the ArrayList method contains the following method trimToSize()with a description: Trims the capacity of this ArrayList instance to be the list's current size. Explain why you would want to trim the capacity of an ArrayList object to its current size. [10%]

Explanation / Answer

a) i) void add(int index, E element):
As you add items to an ArrayList, the list checks to see if the backing array has room left.
If there is room, the new item is just added at the index specified. If there is not room, a new, larger, array is created, and the old array is copied into the new one.
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right.

public void add(int index, E element) {
   checkforRange(index);
   checkCapacity(size + 1);
   copyarray(elementData, index, elementData, index + 1,size - index);
   elementData[index] = element;
   size++;
}

ii) E remove(int index):
Removes the element at the specified position in this list. Shifts any subsequent elements to the left.

public E remove(int index) {
   checkRange(index);
   count++;
   E oldValue = elementData(index);
   int numMoved = size - index - 1;
   if (numMoved > 0)
       System.arraycopy(elementData, index+1, elementData, index,numMoved);
   elementData[--size] = null; // garbage collector do its work
   return oldValue;
}

iii) E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
public E set(int index, E element) {
   checkRange(index);
   E oldValue = elementData(index);
   elementData[index] = element;
   return oldValue;
}

b) when you know the size of your array and you are sure that your array will not grow/shrink, you can use array instead of arraylist.
When you know that the data that your are going to store is primitive data and of fixed size then go for array.

c) It's the initial capacity, i.e. the number of items that ArrayList will allocate to begin with as the internal storage of items.
ArrayList can contain "any number of items" (as per your memory) and when doing large initial insertions you can tell ArrayList to allocate a larger storage to begin with as to not waste CPU cycles when it tries to allocate more space for the next item.

d) We can use this operation to minimize the storage of an ArrayList instance. (As it grow dynamically to a size 1.5 times greater than the initial size in add() operation when the initial capacity is filled).