Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Double-Linked Lists and Circular Lists
The List ADT Textbook Sections
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.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
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.
Professor Evan Korth (adapted from Sun’s collections documentation)
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.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
1 Collections Working with More than One Number or Data Type or Object.
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.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Arrays And ArrayLists - S. Kelly-Bootle
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
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.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Fifteen: An Introduction to Data Structures.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. An Introduction to Data Structures.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
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.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Chapter 15 An Introduction to Data Structures. Chapter Goals To learn how to use the linked lists provided in the standard library To be able to use iterators.
List Interface and Linked List Mrs. Furman March 25, 2010.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 15 – An Introduction to Data Structures.
Data Structures Lakshmish Ramaswamy. Removing Element public Object remove(int index){ Object retobj; if(index size){ throw IndexOutOfBoundsException.
Collections Dwight Deugo Nesa Matic
Java Collections CHAPTER 3-February-2003
EKT472: Object Oriented Programming
Chapter 20 An Introduction to Data Structures
Introduction to Data Structures
Linked Lists.
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Lecture 6: Collections.
Introduction to Collections
Computer Science and Engineering
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
Introduction to Collections
Part of the Collections Framework
Java Generics & Iterators
Presentation transcript:

Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode

Java Collection Framework A collection is an object that represents a group of objects A collections framework is just a unified design used for representing and manipulating collections

What does the Collections Framework consist of? Collection interfaces General-purpose implementations (primary implementations) Legacy implementations (collection classes for earlier releases) Wrapper implementations Convenience implementations Abstract implementations Algorithms (static methods that perform useful functions such as sorting) Infrastructure Array Utilities

Core Collection Interfaces

Collection The most basic interface in the collections framework Set, List and SortedSet all extend Collection Map and SortedMap do not extend Collection

Collection Interface - methods boolean add(Object o)addObject boolean addAll(Collection c)addAllCollection void clear()clear boolean contains(Object o)containsObject boolean containsAll(Collection c)containsAllCollection boolean equals(Object o)equalsObject int hashCode()hashCode boolean isEmpty()isEmpty IteratorIterator iterator()iterator boolean remove(Object o)removeObject boolean removeAll(Collection c)removeAllCollection boolean retainAll(Collection c)retainAllCollection int size()size ObjectObject[] toArray()toArray ObjectObject[] toArray(Object[] a) toArrayObject

List Interface A list is an ordered collection or a sequence. ArrayList implements the List interface The user of this interface will have control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

ArrayList Resizable array implementation of the List interface

Methods void add(int index, Object x)addObject boolean add(Object x)addObject Object set(int index, Object x) setObject Object remove(int index)remove int size ()size Object get( (int index) get

How the methods work add:  boolean add(Object x) – inserts the Object x at the end of the list (size increases by 1), returns true  void add(int index, Object x) – inserts the Object x at the given index position (elements will be shifted to make room and size increases by 1)

How the methods work get:  returns the Object at the specified index  should cast when using value returned  throws IndexOutOfBoundsException if index =size

How the methods work set  replaces value of Object parameter at the given index  size is not changed

How the methods work remove  removes the element at the specified index  throws IndexOutOfBoundsException if index =size  size will be decreased by 1  returns Object removed

Examples ArrayList club = new ArrayList(); club.add(“Spanky”); club.add(“Darla”); club.add(“Buckwheat”); System.out.print(club); Displays: [Spanky, Darla, Buckwheat]

//using club from previous slide club.set(1, “Mikey”); System.out.print(club); Displays: [Spanky, Mikey, Buckwheat]

//using club from previous slide club.add(0, club.remove(club.size()-1)); System.out.print(club); Displays: [Buckwheat, Spanky, Mikey]

//ArrayLists only contain Objects!! ArrayList odds = new ArrayList(); for(int i=1; i<10; i+=2) odds.add(new Integer(i)); System.out.println(odds); Displays: [1, 3, 5, 7, 9]

Linked Lists LinkedList class in the java.util package Each element is “linked” by a reference to the next element: LinkedList data NULL link data

Some Methods in LinkedList void addFirst(Object obj) void addLast(Object obj) Object getFirst() Object getLast() Object removeFirst() Object removeLast()

Middle of List?? ListIterator Used as a pointer between two links Initially it points to the first element EX: LinkedList list = new LinkedList(); ListIterator iterator = list.listIterator(); To get the next element: if (iterator.hasNext()) //make sure there is a next! iterator.next(); **the next method will throw a noSuchElementException if there is not a next value

Traversing the whole list while(iterator.hasNext()) { Object obj = iterator.next(); //do something with obj } next returns the object of the link it just passed

Adding Elements The add method adds an object after the iterator then moves the iterator position past the new element iterator.add(“Bob”);

Removing Elements while(iterator.hasNext()) { Object obj = iterator.next(); if( //obj meets some condition ) iterator.remove(); }

Iterators Iterator – Similar to Enumeration interface but more powerful ListIterator – iterator for use with lists.  All functionality of Iterator  Supports bi-directional iteration  Supports element replacement  Supports element retrieval  Supports index retrieval

java.util Interface Iterator Iterators take the place of Enumerations for Collections Iterators differ from Enumerators –  Iterators allow the caller to remove elements from the collection during the iteration

Methods hasNext next remove

public boolean hasNext() Returns true if the iteration has more elements Ex: while(it.hasNext()) //do something

public Object next() Returns the next element in the iteration Each time this method is called the iterator “moves” Ex: while(it.hasNext()) { Object obj = it.next(); if( //obj meets some condition ) //do something }

public void remove() Removes from the collection the last element returned by the iterator Can be called only once per call to next while(it.hasNext()) { Object obj = it.next(); if( //obj meets some condition ) it.remove(); }

Example ArrayList list = new ArrayList(); Random r = new Random(); for(int i = 0; i < 10; i++) list.add(new Integer(r.nextInt(30))); Iterator it = list.iterator(); while(it.hasNext()) { System.out.print(it.next()+ " "); }

ListIterator additional methods add hasPrevious nextIndex previous previousIndex set

ListNode Class provided by College Board Will be in the reference materials given to students during the exam

public class ListNode { private Object value; private ListNode next; public ListNode(Object initValue, ListNode initNext) { value = initValue; next = initNext; } public Object getValue() {return value;} public ListNode getNext() {return next;} public void setValue(Object theNewValue) {value = theNewValue;} public void setNext(ListNode theNewNext) {next = theNewNext;} }

Creating a LinkedList class with ListNode Constructors addFirst addLast add remove - don’t forget special cases! print

Getting Started public class LList { private ListNode front; private ListNode rear; public LList() //constructor {front = null; rear = null; } public void addFirst(Object obj) public void addLast(Object obj) public Object removeFirst() public Object removeLast() public String toString() }

Big-Oh for ArrayList Methods expected run-time worst-case run-time add(obj)O(1) add(index,obj)O(n) get(index)O(1) set(index,obj)O(1) remove(index)O(n)

Big-Oh for LinkedList Methods expected run-time worst-case run-time add(obj)O(1) add(index,obj)O(n) get(index)O(n) set(index,obj)O(n) remove(index)O(n) addFirst/addLast O(1) getFirst/getLastO(1) removeFirst/removelast O(1)

Big-Oh for Iterator expected run-time only Methods ArrayList LinkedList next()O(1) remove()O(n)O(1)

Big-Oh for ListIterator expected run-time only Methods ArrayList LinkedList add(obj)O(n)O(1) set(obj)O(1)