Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 CS 151 Interfaces, lists, and iterators
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Interfaces, lists, and iterators

by admin last modified 2005-05-11 18:13

Interfaces, Lists, and Iterators

Interfaces

Suppose we want to design a class which inherits from two existing classes.  For example, we want to define a StudentEmployee class which inherits from Student and Employee.  This is not possible in Java, because Java permits a class to have only one superclass; that is, it does not support multiple inheritance (C++ does).

Java does, however provide interfaces, which can sometimes be used as an alternative to multiple inheritance.  An interface is similar to an abstract class, with the following differences:
  • It may not have any instance variables.
  • All methods are abstract (whether or not they are explicitly declared as abstract).
example  A Shape interface
interface Shape {
    double perimeter();
    double area();
}
Now, we can define specific shapes, such as:
class Rectangle implements Shape {
    double length, width;
    double perimeter()
    { return 2*length + 2*width; }
    double area()
    {  return length * width; }
}
The application code we saw in the last class will still work:
Shape[] shapes = new Shape[5];

shapes[0] = new Circle(1.0);
shapes[1] = new Triangle(2.0, 2.0, 2.0);
shapes[2] = new Circle(2.5);
shapes[3] = new Rectangle(3.0, 4.0);
shapes[4] = new Rectangle(2.0, 2.0);

double areasum = 0.0;
for(int i = 0 ; i < 5; i++)
    areasum += shapes[i].area();
The Java API contains many useful interfaces, including Comparable, List, and Iterator.


the Comparable Interface

The Comparable interface imposes a total ordering on the objects in each class which implements it.  That is, it makes it possible to compare objects of the class.  The interface consists of one method:
int compareTo(Object obj);
compareTo compares this object to obj, and returns
  • a negative integer if this < obj;
  • zero if this == obj;
  • a positive integer if this > obj;

example  Given two names, print the one which occurs first in lexicographic ordering.

String name1, name2;
if(name1.compareTo(name2)<0)
  System.out.println(name1);
else
  System.out.println(name2);


the List Interface

List is an interface defined in the java.util package.  It is designed to represent a sequential list of objects, with operations such as
  • storing an object at the end of the list
  • storing an object at a particular position in the list
  • inserting an object between two others in the list
  • removing an object from a particular position in the list
  • retrieving the object from a particular position in the list
We'll take a look at some of the interface functions:

boolean add(Object obj);    // adds obj to the end of the list.  Always returns true.  One implication of this is that a list does not have a fixed size.  It is always possible to add one more element to the end of the list.

void add (int index, Object obj);  //  inserts obj at position index.  Items to the right of index are shifted one position to the right to make room for obj.

void clear();  //  delete all the elements from the list.

Object get(int index);  //  returns the Object at position index .  The list is unchanged.

boolean isEmpty();  //  returns true if the list is empty.

Object remove(int index);  //  removes and returns the Object at position index.  Items to the right of index are shifted one position to the left to fill the space left by the deleted object.

Object set(int index, Object obj);  //  stores obj at position index, replacing the Object at that position.  returns the Object which is replaced.

int size();  //  returns a count of the number of elements in the list.

boolean equals(Object obj);  //  compares the list to obj.  returns true if obj is an equivalent list.

Java provides two implementations of the List interface:  ArrayList and LinkedList.

(Note:  Earlier versions of java defined a class called Vector, which represents a list of objects.  Although ArrayList is preferred in current versions of java, Vector is still supported and you may still see it used in applications.)


Iterators

An iterator is an object used to step through a list or collection sequentially.  It is an abstraction of the normal method of stepping through an array, but works on other kinds of list, too.

As defined in Java, Iterator is an interface with two methods:
boolean hasNext();  //  returns true if and only if there are more elements in the list; that is, the iteration is not finished
Object next();  // returns the current element in the list, and advances to the next
ArrayList supports the use of Iterators by supplying a method iterator() which returns an Iterator.  The iterator can then be used to step through the ArrayList.

example  Print an ArrayList of Strings using an Iterator

ArrayList v;

for(Iterator i = v.iterator(); i.hasNext(); )
    System.out.println((String) i.next());

example  Store the first 20 powers of 2 in an array list, then print them with an iterator.





 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: