Back to our Array Based List Topic 6 Started with a list of ints Generic Data Structures Don't want to have to write a new list class for every data type we want to store in lists "Get your data structures correct Moved to an array of Object s to store the first, and the rest of the program will elements of the list write itself." // from array based list - David Jones private Object[] myCon; CS314 Generics 2 Using Object Difficulties with Object In Java, all classes inherit from exactly one Creating generic containers using the Object other class except Object which is at the top data type and polymorphism is relatively of the class hierarchy straight forward Object variables can refer to objects of their Using these generic containers leads to declared type and any descendants some difficulties polymorphism Casting Thus, if the internal storage container is of Type checking type Object it can hold anything Code examples on the following slides primitives handled by wrapping them in objects. int Integer, char - Character CS314 Generics 3 CS314 Generics 4
Clicker Question 1 Code Example - Casting What is output by the following code? Assume a list class GenericList list = new GenericList(); // 1 GenericList li = new GenericList(); String name = "Olivia"; li.add("Hi"); list.add(name); // 2 System.out.println( li.get(0).charAt(0) ); System.out.print( list.get(0).charAt(2) );// 3 // previous line has syntax error A. i // return type of get is Object // Object does not have a charAt method B. No output due to syntax error at line // 1 // compiler relies on declared type C. No output due to syntax error at line // 2 System.out.println( ((String)li.get(0)).charAt(0) ); D. No output due to syntax error at line // 3 // must cast to a String E. No output due to runtime error. CS314 Generics 5 CS314 Generics 6 "Fixing" the Method Code Example type checking //pre: all elements of li are Strings //pre: all elements of li are Strings public void printFirstChar(GenericList li) { public void printFirstChar(GenericList li) { String temp; for(int i = 0; i < li.size(); i++) { for(int i = 0; i < li.size(); i++) { String temp = (String)li.get(i); if( li.get(i) instanceof String ){ if( temp.length() > 0 ) temp = (String)li.get(i); System.out.println( if( temp.length() > 0 ) temp.charAt(0) ); System.out.println( } temp.charAt(0) ); } } // what happens if pre condition not met? } } CS314 Generics 7 CS314 Generics 8
Clicker 2 - Too Generic? Is this a bug or a feature? Does this code compile? GenericList list = new GenericList(); list.add( "Olivia" ); list.add( new Integer(12) ); list.add( new Rectangle(1, 2, 3, 4) ); list.add( new GenericList() ); A. No B. Yes CS314 Generics 9 CS314 Generics 10 Generic Types Making our Array List Generic Data type variables declared in class header Java has syntax for parameterized data types Referred to as Generic Types in most of the public class GenericList<E> { literature The <E> is the declaration of a data type A traditional parameter has a data type and can parameter for the class store various values just like a variable any legal identifier: Foo , AnyType , Element , public void foo(int x) DataTypeThisListStores Sun style guide recommends terse identifiers Generic Types are like parameters, but the data type for the parameter is data type The value E stores will be filled in whenever a programmer creates a new GenericList like a variable that stores a data type this is an abstraction . Actually, all data type info is GenericList<String> li = erased at compile time and replaced with casts and new GenericList<>(); typically variables of type Object CS314 Generics 11 CS314 Generics 12
Modifications to GenericList Modifications to GenericList instance variable Careful with the equals method private E[] myCon; Recall type information is actually erased at compile time. Parameters on At runtime not sure what data type of elements add, insert, remove, insertAll are. (Unless we get into reflection.) Return type on use of wildcard get rely on the elements equals methods Changes to creation of internal storage container myCon = (E[]) new Object[DEFAULT_SIZE]; Constructor header does not change CS314 Generics 13 CS314 Generics 14 Using Generic Types Using Generic Types Back to Java's ArrayList ArrayList<String> list2 = new ArrayList<String>(); ArrayList list1 = new ArrayList(); for list2 E stores String still allowed, a "raw" ArrayList list2.add( "Isabelle" ); works just like our first pass at GenericList System.out.println( casting, lack of type safety list2.get(0).charAt(2) ); //ok list2.add( new Rectangle() ); // syntax error CS314 Generics 15 CS314 Generics 16
Parameters and Generic Types Generic Types and Subclasses ArrayList<Shape> list5 = Old version new ArrayList<Shape>(); //pre: all elements of li are Strings public void printFirstChar(ArrayList li){ list5.add( new Rectangle() ); New version list5.add( new Square() ); //pre: none list5.add( new Circle() ); public void printFirstChar(ArrayList<String> li){ // all okay list5 can store Shape objects and any Elsewhere descendants of Shape ArrayList<String> list3 = new ArrayList<String>(); printFirstChar( list3 ); // ok ArrayList<Integer> list4 = new ArrayList<Integer>(); printFirstChar( list4 ); // syntax error CS314 Generics 17 CS314 Generics 18
Recommend
More recommend