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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started