Using Linked Lists in java'util Package - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Using Linked Lists in java'util Package

Description:

void addFirst(Object ob) Add all the element from the Collection col at the position pos. ... (Collection col) Return true if the list contains the Object ob. ... – PowerPoint PPT presentation

Number of Views:118
Avg rating:3.0/5.0
Slides: 12
Provided by: win1249
Category:

less

Transcript and Presenter's Notes

Title: Using Linked Lists in java'util Package


1
Using Linked Lists in java.util Package
  • Linked Lists in java.util package Introduction.
  • LinkedList class of java.util package
    Introduction.
  • LinkedList class Class Hierarchy.
  • LinkedList class Methods summary.
  • LinkedList class Usage and Applecations.

2
Linked Lists in java.util package Introduction
  • Linked lists as defined in the java.util package
    offer various operations on the nodes of a linked
    list.
  • he proposed implementation in the java.util
    package is defined in a class called LinkedList.
  • The LinkedList class implements a list as a
    generic doubly linked list with head and tail
    references.
  • An instance of such a list that stores Student
    objects is shown below

3
LinkedList class Hierarchy
Object
AbstractCollection
AbstractList
Collection
List
AbstractSequentalList
List
LinkedList
Cloneable
Serializable
4
LinkedList class Methods
  • We summarize in the table below the methods of
    LinkedList class

5
LinkedList class Methods (Contd.)
6
LinkedList class Methods
7
Using LinkedList class
  • Well demonstrate the use of LinkedList class in
    the code shown below
  • 1 import java.util.LinkedList
  • 2 import java.util.Iterator
  • 3
  • 4 public class TestLinkedList
  • 5 public static void main(String args)
  • 6 LinkedList lst1 new LinkedList()
  • 7 lst1.addFirst(new Integer(4))
  • 8 lst1.addFirst(new Integer(5))
  • 9 lst1.addFirst(new Integer(6))
  • 10 lst1.addLast(new Integer(5))
  • 11 System.out.println("The index of Element 5
    is " lst1.indexOf(new Integer(5)))
  • 12 System.out.println("The index of Element 7
    is " lst1.indexOf(newInteger(7)))
  • 13 System.out.println("The first element in the
    list lst1 is " lst1.getFirst())
  • 14 System.out.println("The last element in the
    list lst1 is " lst1.getLast(
  • 15 LinkedList lst2 new LinkedList(lst1)
  • 16 for(int i 0 i lt lst1.size() i)
  • 17 System.out.print(lst1.get(i))
  • 18 System.out.println()

8
Drill Questions
  • Using the JDK documentation, explore the
    remaining methods defined in the LinkedList
    implementation of java.util package. Summarize
    these methods in a table similar to the ones
    presented in this session.
  • Write a test class called TestMyLinkedList to
    test MyLinkedList class we have defined in the
    previous sessions.
  • Write a method called toString() that prints the
    content of any list instance of type MyLinkedList
    defined in the previous sessions.

9
(No Transcript)
10
(No Transcript)
11
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com