Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Thought for the Day

It is not the mountain we conquer,


but ourselves.
– Sir Edmund Hillary
Two Classes for Lists of Integers
• IntegerVector
– based on an array
– efficient access
– may run out of memory, or waste memory
• IntegerList
– based on a linked list of nodes
– efficient, flexible memory use
– may have inefficient access
• Can be used interchangeably by clients
– identical methods
Generic Data Structures
• We have now seen two list classes
– Both for lists of integers

• What about lists of other types?


– We could reimplement either of the integer list
classes

OR: Use polymorphism or generics in Java:


Generic Data Structures
Generic Data Structures
Using Polymorphism
• Consider either of the integer list classes
– We will use the linked-list implementation
• Very few places where it is important that
we are dealing with integers only:
– data member in the ListNode class
– parameters for add, position and set
– return type for get

Replace these with Object


The ObjectList Class
• Convert the IntegerList class to a
generic ADT using polymorphism

• Class Diagram

ObjectList
first, numElements
add, get, set, position,
remove, length, toString
Inner Class and Data Members
public class ObjectList
{ private class ListNode
{ public Object data;
public ListNode next;
} // class ListNode

private ListNode first; // Pointer to the


// first ListNode
private int numElements; // Number of
// elements
. . .
} // class ObjectList
The add Method

public void add (Object item, int position)


// Place the new item in an ObjectList
{ ...
} // add
The get and set Methods
public Object get (int index)
// Access an ObjectList
{ ...
} // get

public void set (int index, Object item)


// Access an ObjectList
{ ...
} // set
The position Method
public int position (Object item)
// Find item in an ObjectList
{ ListNode curr = first; Cannot use !=
int k;
for (k = 0;
curr != null && !curr.data.equals(item);
k++, curr = curr.next)
; // Search for item in ObjectList
if (curr == null) // item was not found
return -1;
else
return k;
} // position
The toString Method
public String toString ()
{ StringBuffer s = new StringBuffer("[");
for (ListNode curr = first;
curr != null;
curr = curr.next)
{ s.append(curr.data.toString());
if (curr.next != null)
s.append(", ");
}
s.append("]");
return s.toString();
} // toString
Using the ObjectList Class
• A list of integers!
ObjectList iList = new ObjectList();
iList.add(3);
iList.add(-7);
iList.add(56);
Using the ObjectList Class
public class Student
{ . . .
} // class Student
...
ObjectList classList = new ObjectList();
classList.add(new Student());
Need to Take Care When Using
get
Student st;
st = classList.get(5);
// Retrieve sixth student
Won’t work!

get returns an Object: must use a type cast

Student st;
st = (Student)classList.get(5);
// Retrieve sixth student
Mixed Lists
ObjectList mixed = new ObjectList();
mixed.add(3); // Integer
mixed.add(new Student());
mixed.add(new Date());
mixed.add(new Random());
mixed.add(”Hello”); // String
Need to take even more care with get!
Student st;
Object obj = mixed.get(k); // Get k’th object
if (obj instanceof Student) // Check type
st = (Student)obj; // Type cast will work
else
...
4.4.2. Java Generics
• Allow us to simplify the type-casts, etc.
required when using polymorphism

• Provide extra safety


– Extensive compile-time checking

• Effectively, introduce a “type parameter”


– “List of objects of type T”
Syntax
public class MyClass<T>
{ . . .
} // class MyClass

• Inside MyClass we can use T as a type


name
– variables
– parameters
– return types
– etc.
The GenericList Class
• Start with ObjectList
– Introduce type parameter
– Replace Object with type parameter

public class GenericList<T>


{ private class ListNode
{ public T data;
public ListNode next;
} // inner class ListNode
. . .
} // class GenericList
The add Method

public void add (T item, int position)


// Place the new item in a GenericList
{ ...
} // add
The get and set Methods
public T get (int index)
// Access an element in a GenericList
{ ...
} // get

public void set (int index, T item)


// Access an element in a GenericList
{ ...
} // set
The position Method

public int position (T item)


// Find item in a GenericList
{ . . .
} // position
Using Generic Classes
• Must specify the actual type to be used
GenericList<Student> classList =
new GenericList<Student>();
. . .
• Compiler checks that only Student
objects are placed in classList
• Compiler can also infer type on the right
GenericList<Student> classList =
new GenericList<>();

You might also like