24List - ITI 1121. Introduction to Computing II * Marcel...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 26, 2011 Abstract Enabling recursive list processing outside of the class * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Generic methods Besides generic types (interfaces and classes with formal type parameters), methods can also have type parameters. public class Utils { public static <E> List<E> toList( E xs ) { List<E> ys; ys = new LinkedList<E>(); for ( E elem : xs ) { ys.add( elem ); } return ys; } } 1) Herein, the class Utils has no type parameter, 2) toList is a class method, 3) toList has a type parameter, 4) the type declaration is local to the method toList . Generic methods public class Utils { public static < T extends Comparable<T> > T max( T a, T b ) { if ( a.compareTo( b ) > 0 ) { return a; } else { return b; } } } Using generic methods Invoking a generic method requires no additional syntactical construction. The type argument is inferred automatically. Integer i1, i2, iMax; i1 = new Integer( 1 ); i2 = new Integer( 10 ); iMax = max( i1, i2 ); System.out.println( "iMax = " + iMax ); Here, the compiler infers the type argument Integer . Using generic methods String s1, s2, sMax; s1 = new String( "alpha" ); s2 = new String( "bravo" ); sMax = max( s1, s2 ); System.out.println( "sMax = " + sMax ); Here, the compiler infers the type argument String . Using generic methods The type may also be supplied explicitely: iMax = Utils.<Integer>max( i1, i2 ); sMax = Utils.<String>max( s1, s2 ); this is necessary whenever the type system cannot infer a type that is specific enough. Generic methods Defining a generic method. public class Test { public static <E> void display( LinkedList<E> xs ) { for ( int i=0; i<xs.size(); i++ ) { System.out.println( xs.get( i ) ); } } } Using a generic method. LinkedList<String> xs; xs = new LinkedList<String>(); xs.add( "alpha" ); xs.add( "bravo" ); xs.add( "charlie" ); Test.display( xs ); Introduction With the current implementation, recursive methods can only be written inside of the class LinkedList . public int size() { return size( first ); } private int size( Node<E> p ) { if ( p == null ) { return 0; } return 1 + size( p.next ); } Discussion Can you propose changes, new methods for instance, enabling recursive list processing outside of the class LinkedList ?...
View Full Document

Page1 / 47

24List - ITI 1121. Introduction to Computing II * Marcel...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online