© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)

Slides:



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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Generics and the ArrayList Class
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
Chapter 16 Collections, Maps and Iterators Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Iterators An iterator is an object that is used with a collection to provide sequential access to.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L16 (Chapter 22) Java Collections.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Chapter 19 Java Data Structures
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
Data structures and algorithms in the collection framework 1 Part 2.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Chapter 18 Java Collections Framework
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Data structures and algorithms in the collection framework 1.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
Arrays, ArrayLists, and Collections. Rationale Suppose we have a program to compute the average of three numbers. We could write a simple little method.
The Java Collections Framework Based on
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
CSE 1201 Object Oriented Programming ArrayList 1.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
University of Limerick1 Collections The Collection Framework.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 19 Java Data Structures
University of Central Florida COP 3330 Object Oriented Programming
Collections class Method name Description binarySearch(list, value)
Collections in Java The objectives of this lecture are:
Shapes Consider the task of writing classes to represent 2D shapes such as Circle, Rectangle, and Triangle. Certain operations are common to all shapes:
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-2 Methods in the List Interface (Part 2 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-3 Methods in the List Interface (Part 3 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-4 Methods in the List Interface (Part 4 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-5 Methods in the List Interface (Part 5 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-6 Methods in the List Interface (Part 6 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-7 Methods in the List Interface (Part 7 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-8 Methods in the List Interface (Part 8 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-9 Methods in the List Interface (Part 9 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-10 Methods in the List Interface (Part 10 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-11 Methods in the List Interface (Part 11 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-12 Methods in the List Interface (Part 12 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-13 Methods in the List Interface (Part 13 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-14 Methods in the List Interface (Part 14 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-15 Methods in the List Interface (Part 15 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-16 Methods in the List Interface (Part 16 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-17 Pitfall: Optional Operations When an interface lists a method as "optional," it must still be implemented in a class that implements the interface –The optional part means that it is permitted to write a method that does not completely implement its intended semantics –However, if a trivial implementation is given, then the method body should throw an UnsupportedOperationException

© 2006 Pearson Addison-Wesley. All rights reserved16-18 Tip: Dealing with All Those Exceptions The tables of methods for the various collection interfaces and classes indicate that certain exceptions are thrown –These are unchecked exceptions, so they are useful for debugging, but need not be declared or caught In an existing collection class, they can be viewed as run-time error messages In a derived class of some other collection class, most or all of them will be inherited In a collection class defined from scratch, if it is to implement a collection interface, then it should throw the exceptions that are specified in the interface

© 2006 Pearson Addison-Wesley. All rights reserved16-19 Concrete Collections Classes The concrete class HashSet implements the Set interface, and can be used if additional methods are not needed –The HashSet class implements all the methods in the Set interface, and adds only constructors –The HashSet class is implemented using a hash table The ArrayList and Vector classes implement the List interface, and can be used if additional methods are not needed –Both the ArrayList and Vector interfaces implement all the methods in the interface List –Either class can be used when a List with efficient random access to elements is needed

© 2006 Pearson Addison-Wesley. All rights reserved16-20 Concrete Collections Classes The concrete class LinkedList is a concrete derived class of the abstract class AbstractList LinkedList class is another concrete class, which uses a private linked list for its elements instead of an array The interface SortedSet and the concrete class TreeSet are designed for implementations of the Set interface that provide for rapid retrieval of elements –The implementation of the class is similar to a binary tree, but with ways to do inserting that keep the tree balanced

© 2006 Pearson Addison-Wesley. All rights reserved16-21 Methods in the HashSet Class (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved16-22 Methods in the HashSet Class (Part 2 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved16-23 Methods in the Classes ArrayList and Vector (Part 1 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-24 Methods in the Classes ArrayList and Vector (Part 2 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-25 Methods in the Classes ArrayList and Vector (Part 3 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-26 Methods in the Classes ArrayList and Vector (Part 4 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-27 Methods in the Classes ArrayList and Vector (Part 5 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-28 Methods in the Classes ArrayList and Vector (Part 6 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-29 Methods in the Classes ArrayList and Vector (Part 7 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-30 Methods in the Classes ArrayList and Vector (Part 8 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-31 Methods in the Classes ArrayList and Vector (Part 9 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-32 Methods in the Classes ArrayList and Vector (Part 10 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-33 Methods in the Classes ArrayList and Vector (Part 11 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-34 Methods in the Classes ArrayList and Vector (Part 12 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-35 Methods in the Classes ArrayList and Vector (Part 13 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-36 Methods in the Classes ArrayList and Vector (Part 14 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-37 Methods in the Classes ArrayList and Vector (Part 15 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-38 Differences Between ArrayList and Vector For most purposes, the ArrayList and Vector are equivalent –The Vector class is older, and had to be retrofitted with extra method names to make it fit into the collection framework –The ArrayList class is newer, and was created as part of the Java collection framework –The ArrayList class is supposedly more efficient than the Vector class also

© 2006 Pearson Addison-Wesley. All rights reserved16-39 The Collections Class The Collections class is a utility class. It contains static methods for manipulating collection objects. The Arrays class is also a utility class. It contains static methods for manipulating arrays. One useful method used is asList() to convert an array into a list. The following are the most frequently used methods of the Collections class. –static int binarySearch(List list, Object o) –static int binarySearch(List list, Object o, Comparator c) –static void sort(List list) –static void sort(List list, Comparator c) –static Object max(Collection c) –static Object min(Collection c) –static void reverse(List list) –static void shuffle(List list)