3. ArrayList
• The ArrayList class extends AbstractList and implements the List
interface.
• Defined in java.util package.
• ArrayList supports dynamic arrays that can grow as needed.
• Array lists are created with an initial size.
• When this size is exceeded, the collection is automatically enlarged.
• When objects are removed, the array may be shrunk.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
4. ArrayList Constructors
• T he ArrayList class supports three constructors.
ArrayList() : creates an empty array list with an initial capacity
sufficient to hold 10 elements.
ArrayList(int capacity) : creates an array list that has the
specified initial capacity.
ArrayList(Collection c) : creates an array list that is initialized
with the elements of the collection c.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
5. Methods of ArrayList
boolean add(Object o)
• Appends the specified element to the end of this list.
void add(int index, Object element)
• Inserts the specified element at the specified position index in
this list.
• Throws IndexOutOfBoundsException if the specified index is
out of range.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
6. boolean addAll(Collection c )
• Appends all of the elements in the specified collection to the end of
this list.
• Throws NullPointerException if the specified collection is null.
boolean addAll(int index, Collection c )
• Inserts all of the elements of the specified collection into this list,
starting at the specified position.
• Throws NullPointerException if the specified collection is null.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
7. void clear()
• Removes all of the elements from this list.
Object remove(int index)
• Removes the element at the specified position in this list.
Object clone()
• Returns a shallow copy of this ArrayList.
int size()
• Returns the number of elements in the list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
8. boolean contains(Object o)
• Returns true if this list contains the specified element.
Object get(int index)
• Returns the element at the specified position in this list.
int indexOf(Object o)
• Returns the index in this list of the first occurrence of the
specified element, or -1 if the List does not contain the element.
int lastIndexOf(Object o)
• Returns the index in this list of the last occurrence of the
specified element, or -1.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
9. void ensureCapacity(int minCapacity)
• Increases the capacity of this ArrayList instance, if necessary,
to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
Object set(int index, Object element)
• Replaces the element at the specified position in this list with
the specified element.
• Throws IndexOutOfBoundsException if the specified index is
out of range (index < 0 || index >= size()).
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
10. void trimToSize()
• Trims the capacity of this ArrayList instance to be the list’s
current size.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
12. LinkedList
• LinkedList stores elements in a linked list.
• Insertion or deletion of elements at the beginning of the list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
13. LinkedList Constructors
• T he LinkedList class supports two constructors.
LinkedList() : creates an empty list with an initial capacity
sufficient to hold 10 elements.
LinkedList(Collection c) : creates an array list that is
initialized with the elements of the collection c.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
14. Methods of LinkedList
• void addFirst(Object o)
• void addLast(Object o)
• Object getFirst()
• Object getLast()
• Object removeFirst()
• Object removeLast()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
15. Difference b/w LinkedList and ArrayList
• The critical difference between them pertains to internal
implementation, which affects their performance.
• ArrayList is efficient for retrieving elements and LinkedList is
efficient for inserting and removing elements at the beginning
of the list.
• Both have the same performance for inserting and removing
elements in the middle or at the end of the list.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
17. Vector
• Vector is a subclass of AbstractList.
• Vector is the same as ArrayList, except that it contains
synchronized methods for accessing and modifying the vector.
• Synchronized methods can prevent data corruption when a
vector is accessed and modified by two or more threads
concurrently.
• Applications that do not require synchronization, using
ArrayList is more efficient than using Vector.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
18. Vector Constructors
• Vector class supports following constructors.
Vector()
Vector(Collection c) :
Vector(int capacity)
Vector(int capacity, int increment): Creates a vector with the
specified initial capacity and increment.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
19. Methods of Vector
• void addElement (Object o)
• void elementAt(int index)
• Enumerations elements()
• void ensureCapacity(int capacity)
• Object firstElement()
• Object lastElement()
• void insertElementAt(Object o, int index)
• int capacity()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
20. Methods of Vector
• void copyInto(Object [])
• void removeAllElements()
• void removeElement(Object o)
• void removeElementAt(int index)
• setElementAt(Object o, int index)
• setSize(int size)
• void trimToSize()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
21. Difference b/w Vector and ArrayList
• Vector is synchronized where as ArrayList is not.
• ArrayList supports Iterator and ListIterator whereas Vector
supports Enumeration and Iterator.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
22. Stack
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
23. Stack
• Vector and Stack are the legacy classes.
• There were introduced prior to Java 2.
• Stack is a subclass of Vector.
• Stack class defines the default constructor only:
Stack()
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
24. Methods of Stack
• boolean empty()
• Object peek()
• Object pop()
• Object push(Object o)
• int search(Object o) : Returns the position of the specified
element in this stack.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)