CS226 -- Day 11 - Spring 2013 GENERICS --------------------------------------------------- - declaring static generic methods: public static returnType method(T thing) - declaring generic interfaces & classes: public interface MyGen - uses one generic class T public class MyDoubleGen - uses two generic types - Generics can have bounds to qualify groups of appropriate types - - use ? as wildcard - can be - can be > (ie, for ordered collections) - Implementing generics: - you cannot create an array of a generic type: T[] ra = new T[10]; - instead create an array of object and typecast: T[] ra = (T[]) new Object[10] however this creates an unavoidable compiler warning - Using generics: BaG b = new BaGArray(); BaG b = new BaGArray(); See BaG.java and BaGArray.java for Bags using generics In addition to causing problems with generics, using arrays can be limiting because of the need to resize periodically as they get filled up. Now we introduce our first dynamic data structure: linked list. LINKED LISTS ------------------------------------------ http://en.wikipedia.org/wiki/Linked_list Basic Concept - nodes contain data and links to neighboring nodes - "head" of list is a reference to the first node (or null if empty) - can dynamically increase and decrease as data is added and removed - can add or delete with a few constant operations (no shifting) once the place in the list is found - does not have random access to individual data elements like an array - singly linked list with head pointer to first node is simplest form head |node1 |node2 |node3 ... |nodex |nodeLast --> | --> | --> | --> ... | --> | null Implementation Variations - keep "tail" reference to the last node, makes adding to the end fast - keep sentinel empty (dummy) nodes at beginning and end to avoid special cases in code - make doubly linked list with forward and backwards links - any combination of the above EXAMPLE: - SLList.java - generic singly linked list implementation