list classes

25
Advanced Java Programming ArrayList, LinkedList ,Vector and Stack By Ravi Kant Sahu Asst. Professor, LPU

Upload: ravikantsahu

Post on 20-May-2015

341 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: List classes

Advanced Java ProgrammingArrayList, LinkedList ,Vector and Stack

ByRavi Kant Sahu

Asst. Professor, LPU

Page 2: List classes

ArrAyList

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 3: List classes

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)

Page 4: List classes

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)

Page 5: List classes

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)

Page 6: List classes

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)

Page 7: List classes

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)

Page 8: List classes

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)

Page 9: List classes

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)

Page 10: List classes

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)

Page 11: List classes

LinkedList

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 12: List classes

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)

Page 13: List classes

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)

Page 14: List classes

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)

Page 15: List classes

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)

Page 16: List classes

Vector

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 17: List classes

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)

Page 18: List classes

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)

Page 19: List classes

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)

Page 20: List classes

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)

Page 21: List classes

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)

Page 22: List classes

Stack

Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)

Page 23: List classes

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)

Page 24: List classes

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)

Page 25: List classes