Section 24.3 Check Point Questions7 questions
24.3.1
What are the limitations of the array data type?
24.3.2
MyArrayList is implemented using an array, and an array is a fixed-size data structure. Why is MyArrayList considered a dynamic data structure?
24.3.3
Show the length of the array in MyArrayList after each of the following statements is executed.
1 MyArrayList<Double> list = new MyArrayList<>(); 2 list.add(1.5); 3 list.trimToSize(); 4 list.add(3.4); 5 list.add(7.4); 6 list.add(17.4);
24.3.4
What is wrong if lines 11-12 in LiveExample 24.2, MyArrayList.java,
for (int i = 0; i < objects.length; i++) add(objects[i]);are replaced by
data = objects; size = objects.length;
24.3.5
If you change the code in line 33 in LiveExample 24.2, MyArrayList.java, from
E[] newData = (E[])(new Object[size * 2 + 1]);to
E[] newData = (E[])(new Object[size * 2]);the program is incorrect. Can you find the reason?
24.3.6
Will the MyArrayList class have memory leak if the following code in line 41 is deleted?
data = (E[])new Object[INITIAL_CAPACITY];
24.3.7
The get(index) method invokes the checkIndex(index) method
(lines 59-63 in LiveExample 24.2) to throw
an IndexOutOfBoundsException if the index is out of bounds.
Suppose the add(index, e) method is implemented as follows:
public void add(int index, E e) { checkIndex(index); // Same as lines 23-33 in LiveExample 24.2 MyArrayList.java }What will happen if you run the following code?
MyArrayList<> list = new MyArrayList<>(); list.add("New York");