Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Linked Lists in java.util Package Linked Lists in java.util package: Introduction. LinkedList class of java.util package: Introduction. LinkedList.

Similar presentations


Presentation on theme: "Using Linked Lists in java.util Package Linked Lists in java.util package: Introduction. LinkedList class of java.util package: Introduction. LinkedList."— Presentation transcript:

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: ICS Saleh Fouad 994785 Student COE Ali Juma’ah 987458 Student EE Saleh Fouad 201547 Student dlist head tail Node1 Node2 Node3

3 LinkedList class: Hierarchy Collection Object AbstractCollection AbstractSequentalList AbstractList List Cloneable List LinkedList Serializable

4 LinkedList class: Methods We summarize in the table below the methods of LinkedList class: operationMethod Name Insret an Object ob at the end of the list.void add(Object ob) Insret an Object ob at the position posvoid add(int pos, Object ob) Add all the element from the Collection col at the end of the list boolean addAll(Collection col) Add all the element from the Collection col at the position pos. boolean addAll(int pos, Collection col) Insret an Object ob at the beginning of the list.void addFirst(Object ob) Insret an Object ob at the end of the list.void addLast(Object ob) Remove all the objects from the list.void clear()

5 LinkedList class: Methods (Contd.) operationMethod Name Returns the copy of the list without cloning its elements.Object clone() Return true if the list contains the Object ob.boolean Contains(Object ob) Return true if the list contains all the Objects in the collection col. boolean ContainsAll(Collection col) Return true if the current list and the Object lst contain equal Objects with the same order. boolean equals(Object lst) Return the object at position pos.Object get(position pos) Return the first object in the list.Object getFirst() Return the last object in the list.Object getLast()

6 LinkedList class: Methods operationMethod Name Returns the index of the Object ob.int indexOf() Return true if the list is empty.boolean isEmpty() Generate and returns an iterator for the list.Iterator iterator() Return true if the current list and the Object lst contain equal Objects with the same order. boolean equals(Object lst) Creates an empty list.LinkedList() Creates a list with copies of elements from.LinkedList(Collection col) Return the number of objects in the list.int size()

7 Using LinkedList class We’ll 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 < lst1.size(); i++) 17 System.out.print(lst1.get(i)); 18 System.out.println(); 19 for(Iterator it = lst1.iterator(); it.hasNext(); ) 20 System.out.print(it.next() + " "); 21 System.out.println(); 22 } // End of main() method 23 } // End of TestLinkedList Class

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

10

11


Download ppt "Using Linked Lists in java.util Package Linked Lists in java.util package: Introduction. LinkedList class of java.util package: Introduction. LinkedList."

Similar presentations


Ads by Google