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

Slides:



Advertisements
Similar presentations
The List ADT Textbook Sections
Advertisements

Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Doubly Linked Lists Doubly Linked Lists: Introduction. Doubly Linked Lists: Implementation Doubly Linked Lists: Analysis Doubly Linked Lists: Creation.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
15-Jun-15 Lists in Java Part of the Collections Framework.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 The Collection Interface public interface Collection extends Iterable { boolean add(E e); boolean addAll(Collection c); void clear(); boolean contains(Object.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
The Java Collections Package C. DeJong Fall 2001.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
LinkedList Many slides from Horstmann modified by Dr V.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Java linked list.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Data Structures Lakshmish Ramaswamy. Removing Element public Object remove(int index){ Object retobj; if(index size){ throw IndexOutOfBoundsException.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Fundamental of Java Programming
The List ADT Reading: Textbook Sections 3.1 – 3.5
COP 3503 FALL 2012 Shayan Javed Lecture 8
Linked Lists.
Programming in Java Lecture 11: ArrayList
The List ADT Reading: Textbook Sections 3.1 – 3.5
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Computer Science and Engineering
Collections Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Hashing in java.util
Matrix and Linked List ADTs
Java Generics & Iterators
Presentation transcript:

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.

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

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

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()

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()

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()

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

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.