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.

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%]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!