ArrayList ensureCapacity implementation

This Java program is to Implement ArrayList Collection API. Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. [This class is roughly equivalent to Vector, except that it is unsynchronized.]
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O[n] time. All of the other operations run in linear time [roughly speaking]. The constant factor is low compared to that for the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.

Here is the source code of the Java program to Implement ArrayList Collection API. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. import java.util.ListIterator;
  7. import java.util.Set;
  8. public class ArrayListImpl
  9. {
  10. private ArrayList arrayList;
  11. /* Constructs an empty list with an initial capacity of ten. */
  12. public ArrayListImpl[]
  13. {
  14. arrayList = new ArrayList[];
  15. }
  16. /*
  17. * Constructs a list containing the elements of the specified collection, in
  18. * the order they are returned by the collection's iterator.
  19. */
  20. public ArrayListImpl[Collection

Chủ Đề