Lists and the collection interface review inheritance collections
1 / 73

Lists and the Collection Interface Review inheritance & collections - PowerPoint PPT Presentation

  • Uploaded on

Lists and the Collection Interface Review inheritance & collections. Objectives. Theory: To understand list, linked list To study the difference between single-, double-, and circular linked list data structures Implementation: List interface, ArrayList , LinkedList

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Lists and the Collection Interface Review inheritance & collections' - zorana

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lists and the collection interface review inheritance collections

Lists and the Collection InterfaceReview inheritance & collections


  • Theory:

    • To understand list, linked list

    • To study the difference between single-, double-, and circular linked list data structures

  • Implementation:

    • List interface, ArrayList, LinkedList

    • Review Java inheritance and collection

Insert in the middle of linkedlist
Insert in the middle of Linkedlist



Review question
Review question

______is a version of a linked list in which nodes can be navigated in the forward direction only.

  • Doubly Linked List

  • Singly Linked List

  • Circular Linked List

  • All of the above

Review question1
Review question

______is a version of a linked list in which nodes can be navigated in forward direction only.

  • Doubly Linked List

  • Singly Linked List

  • Circular Linked List

  • All of the above

The list interface and arraylist class
The List Interface and ArrayList Class

  • An array is an indexed structure: can select its elements in arbitrary order using a subscript value

  • Elements may be accessed in sequence using a loop that increments the subscript

  • You cannot

    • Increase or decrease the length

    • Add an element at a specified position without shifting the other elements to make room

    • Remove an element at a specified position without shifting other elements to fill in the resulting gap

Implementation list interface1
Implementation: List interface

  • boolean add(Object o)

  • void clear()

  • boolean contains(Object o)

  • Object get(int index)

  • intindexOf(Object o)

  • booleanisEmpty()

  • Iteratoriterator()

  • intlastIndexOf(Object o)

  • ListIteratorlistIterator()

  • ListIteratorlistIterator(int index)

  • Object remove(int index)

  • boolean remove(Object o)

  • int size()

  • Object[] toArray()

Implementation list interface2
Implementation: List Interface

  • Allowed operations on the List interface include:

    • Finding a specified target

    • Adding an element to either end

    • Removing an item from either end

    • Traversing the list structure without a subscript

  • An array provides the ability to store primitive-type data whereas the List classes all store references to Objects

Review question2
Review question

A method that does not alter a linked list but simply looks at it to determine whether it’s empty, is referred as ________method

  • isClear()

  • isNull()

  • isEmpty()

  • isZero()

Review question3
Review question

A method that does not alter a linked list but simply looks at it to determine whether it’s empty, is referred as ________method

  • isClear()

  • isNull()

  • isEmpty()

  • isZero()

Array based list implementation arraylist class1
Array-based list implementation.ArrayListClass

  • Simplest class that implements the List interface

  • Improvement over an array object

  • Used when a programmer wants to add new elements to the end of a list but still needs the capability to access the elements stored in the list in arbitrary order

Arraylist class
ArrayList Class

Application of arraylist
Application of ArrayList

  • The ArrayList gives you additional capability beyond what an array provides

  • ArrayList stores items of type Object and can thus store an object of any class

  • You cannot store values of the primitive types directly but must instead use wrapper classes

  • When an object is stored in an ArrayList, the programmer must remember the original type

Single linked lists and double linked lists
Single-Linked Lists and Double-Linked Lists

  • ArrayList: add and remove methods operate in linear time because they require a loop to shift elements in the underlying array

    • Linked list overcomes this by providing ability to add or remove items anywhere in the list in constant time

  • Each element (node) in a linked list stores information and a link to the next, and optionally previous, node

A list node1
A List Node

  • A node contains a data item and one or more links

  • A link is a reference to a node

  • A node is generally defined inside of another class, making it an inner class

  • The details of a node should be kept private

Double linked lists
Double-Linked Lists

  • Limitations of a single-linked list include:

    • Can insert a node only after a referenced node

    • Can remove a node only if we have a reference to its predecessor node

    • Can traverse the list only in the forward direction

  • Above limitations removed by adding a reference in each node to the previous node (double-linked list)

Double linked lists2
Double-Linked Lists

Chapter 4: Lists and the Collection Interface

Circular lists
Circular Lists

  • Circular-linked list: link the last node of a double-linked list to the first node and the first to the last

  • Advantage: can traverse in forward or reverse direction even after you have passed the last or first node

    • Can visit all the list elements from any starting point

  • Can never fall off the end of a list

  • Disadvantage: infinite loop!

Linkedlist class

  • Part of the Java API

  • Implements the List interface using a double-linked list

The iterator interface
The Iterator Interface

  • The interface Iterator is defined as part of API package java.util

  • The List interface declares the method iterator, which returns an Iterator object that will iterate over the elements of that list

  • An Iterator does not refer to or point to a particular object at any given time

Iterator interface
Iterator Interface

Chapter 3: Inheritance and Class Hierarchies

The listiterator interface
The ListIterator Interface

  • Iterator limitations

    • Can only traverse the List in the forward direction

    • Provides only a remove method

    • Must advance an iterator using your own loop if starting position is not at the beginning of the list

  • ListIterator is an extension of the Iterator interface for overcoming the above limitations

  • Iterator should be thought of as being positioned between elements of the linked list

  • Comparison of iterator and listiterator
    Comparison of Iterator and ListIterator

    • ListIterator is a subinterface of Iterator; classes that implement ListIterator provide all the capabilities of both

    • Iterator interface requires fewer methods and can be used to iterate over more general data structures

    • Iterator is required by the Collection interface, whereas the ListIterator is required only by the List interface

    Conversion between a listiterator and an index
    Conversion between a ListIterator and an Index

    • ListIterator has the methods nextIndex and previousIndex, which return the index values associated with the items that would be returned by a call to the next or previous methods

    • The LinkedList class has the method listIterator(int index)

      • Returns a ListIterator whose next call to next will return the item at position index

    The collection hierarchy
    The Collection Hierarchy

    • Both the ArrayList and LinkedList represent a collection of objects that can be referenced by means of an index

    • The Collection interface specifies a subset of the methods specified in the List interface

    • Collection interface is the root of the collection hierarchy

      • Two branches: one rooted by the List interface and the other by the Set interface

    Common features of collections
    Common Features of Collections

    • Collection interface specifies a set of common methods

    • Fundamental features include:

      • Collections grow as needed

      • Collections hold references to objects

      • Collections have at least two constructors

    Introduction to inheritance and class hierarchies
    Introduction to Inheritance and Class Hierarchies

    • Popularity of OOP is that it enables programmers to reuse previously written code saved as classes

    • All Java classes are arranged in a hierarchy, starting with Object, which is the superclass of all Java classes

    • Inheritance in OOP is analogous to inheritance in humans

    • Inheritance and hierarchical organization allow you to capture the idea that one thing may be a refinement or extension of another

    Is a versus has a relationships
    Is-a Versus Has-a Relationships (continued)

    • One misuse of inheritance is confusing the has-a relationship with the is-a relationship

    • The has-a relationship means that one class has the second class as an attribute

    • We can combine is-a and has-a relationships

    • The keyword extends specifies that one class is a subclass of another

    A superclass and a subclass
    A Superclass and a Subclass (continued)

    • Consider two classes: Computer and Laptop

    • A laptop is a kind of computer and is therefore a subclass of computer

    Initializing data fields in a subclass and the no parameter constructor
    Initializing Data Fields in a Subclass and the No-Parameter Constructor

    • Private data fields belonging to a base class must be initialized by invoking the base class’s constructor with the appropriate parameters

    • If the execution of any constructor in a subclass does not invoke a superclass constructor, Java automatically invokes the no-parameter constructor for the superclass

      • Initializes that part of the object inherited from the superclass before the subclass starts to initialize its part of the object

    Protected visibility for superclass data fields
    Protected Visibility for Superclass Data Fields Constructor

    • Private data fields are not accessible to derived classes

    • Protected visibility allows data fields to be accessed either by the class defining it or any subclass

    • In general, it is better to use private visibility because subclasses may be written by different programmers and it is always good practice to restrict and control access to the superclass data fields

    Method overriding
    Method Overriding Constructor

    • If a derived class has a method found within its base class, that method will override the base class’s method

    • The keyword super can be used to gain access to superclass methods overridden by the base class

    • A subclass method must have the same return type as the corresponding superclass method

    Method overloading
    Method Overloading Constructor

    • Method overloading: having multiple methods with the same name but different signatures in a class

    • Constructors are often overloaded

    • Example:

      • MyClass(int inputA, int inputB)

      • MyClass(float inputA, float inputB)

    Polymorphism Constructor

    • A variable of a superclass type can reference an object of a subclass type

    • Polymorphism means many forms or many shapes

    • Polymorphism allows the JVM to determine which method to invoke at run time

    • At compile time, the Java compiler can’t determine what type of object a superclass may reference but it is known at run time

    Abstract classes assignment and casting in a hierarchy
    Abstract Classes, Assignment, and Casting in a Hierarchy Constructor

    • An interface can declare methods but does not provide an implementation of those methods

      • Methods declared in an interface are called abstract methods

    • An abstract class can have abstract methods, data fields, and concrete methods

    • Abstract class differs from a concrete class in that

      • An abstract class cannot be instantiated

      • An abstract class can declare abstract methods, which must be implemented in its subclasses

    Abstract classes and interfaces
    Abstract Classes and Interfaces Constructor

    • Like an interface, an abstract class can’t be instantiated

    • An abstract class can have constructors to initialize its data fields when a new subclass is created

      • Subclass uses super(…) to call the constructor

    • May implement an interface but it doesn’t have to define all of the methods declared in the interface

      • Implementation is left to its subclasses

    Class object casting and cloning
    Class Object, Casting and Cloning Interfaces

    • Object is the root of the class hierarchy; every class has Object as a superclass

    • All classes inherit the methods defined in class Object but may be overridden

    The method tostring
    The Method toString Interfaces

    • You should always override the toString method if you want to represent an object’s state

    • If you do not override it, the toString method for class Object will return a string…just not the string you want or are expecting

    Operations determined by type of reference variable
    Operations Determined by Type of Reference Variable Interfaces

    • A variable can reference an object whose type is a subclass of the variable type

    • The type of reference, not the type of the object referenced, determines what operations can be performed

    • Java is a strongly typed language so the compiler always verifies that the type of the expression being assigned is compatible with the variable type

    Casting in a class hierarchy
    Casting in a Class Hierarchy Interfaces

    • Java provides casting to enable us to process one object referenced by one type through a reference variable of its actual type

    • Casting does not change the object referenced; it creates an anonymous reference to that object

    • Downcast: cast a higher type to a lower type

    • The instanceof operator can guard against ClassCastException errors

    • You can downcast an interface reference to the specific implementation type

    The method object equals
    The Method Object.equals Interfaces

    • The Object.equals method has a parameter of type Object

    • Compares two objects to determine whether they are equal

    • You must override the equals method if you want to be able to compare two objects of a class

    Cloning Interfaces

    • The purpose of cloning in object-oriented programming is analogous to cloning in biology

      • Create an independent copy of an object

    • Initially, both objects will store the same information

    • You can change one object without affecting the other

    The object clone method
    The Object.clone method Interfaces

    • Java provides the Object.clone method to help solve the shallow copy problem

    • The initial copy is a shallow copy as the current object’s data fields are copied

    • To make a deep copy, you must create cloned copies of all components by invoking their respective clone methods

    Multiple inheritance multiple interfaces and delegation
    Multiple Inheritance, Multiple Interfaces, and Delegation Interfaces

    • Multiple inheritance: the ability to extend more than one class

    • Multiple inheritance is a language feature that is difficult to implement and can lead to ambiguity

      • Therefore, Java does not allow a class to extend more than one class

    Using multiple interfaces to emulate multiple inheritance
    Using Multiple Interfaces to Emulate Multiple Inheritance Interfaces

    • If we define two interfaces, a class can implement both

    • Multiple interfaces emulate multiple inheritance

    Implementing reuse through delegation
    Implementing Reuse Through Delegation Interfaces

    • You can reduce duplication of modifications and reduce problems associated with version control through a technique known as delegation

    • In delegation, a method of one class accomplishes an operation by delegating it to a method of another class

    Packages Interfaces

    • The Java API is organized into packages

    • The package to which a class belongs is declared by the first statement in the file in which the class is defined using the keyword package followed by the package name

    • All classes in the same package are stored in the same directory or folder

    • All the classes in one folder must declare themselves to be in the same package

    • Classes that are not part of a package may access only public members of classes in the package

    Chapter 3: Inheritance and Class Hierarchies

    The no package declared environment and package visibility
    The No-Package-Declared Environment and Package Visibility Interfaces

    • There exists a default package

      • Files that do specify a package are considered part of the default package

    • If you don’t declare packages, all of your packages belong to the same, default package

    • Package visibility sits between private and protected

      • Classes, data fields, and methods with package visibility are accessible to all other methods of the same package but are not accessible to methods outside of the package

      • Classes, data fields, and methods that are declared protected are visible to all members of the package

    Chapter 3: Inheritance and Class Hierarchies

    Visibility supports encapsulation
    Visibility Supports Encapsulation Interfaces

    • The rules for visibility control how encapsulation occurs in a Java program

    • Private visibility is for members of a class that should not be accessible to anyone but the class, not even the classes that extend it

    • Package visibility allows the developer of a library to shield classes and class members from classes outside the package

    • Use of protected visibility allows the package developer to give control to other programmers who want to extend classes in the package