However the type of the return value is object which

This preview shows page 39 - 57 out of 101 pages.

However, the type of the return value is Object , which forces the caller to cast the type of that value.
Image of page 39

Subscribe to view the full document.

Pair public class Pair { private ______ first; private ______ second; public Pair( ______ first, ______ second ) { this.first = first; this.second = second; } public ______ getFirst( ) { return first; } public ______ getSecond( ) { return second; } }
Image of page 40
Pair public class Pair<T> { private T first; private T second; public Pair( T first, T second ) { this.first = first; this.second = second; } public T getFirst( ) { return first; } public T getSecond( ) { return second; } }
Image of page 41

Subscribe to view the full document.

Pair Pair<String> name; name = new Pair<String>( "Hilary", "Clinton" ); Pair<Time> times; name = new Pair<Time>( new Time( 10,0,0 ), new Time( 11,30,0 ) ); String s; s = name.getFirst(); Time t; t = times.getFirst();
Image of page 42
ArrayStack with Generics Stack<String> s1; name = new ArrayStack<String>( 100 ); Stack<Time> s2; name = new ArrayStack<Time>( 1024 ); s1.push( "alpha" ); s2.push( new Time( 23,0,0 ) ); String a; a = s1.pop();
Image of page 43

Subscribe to view the full document.

ArrayStack with Generics What are the required changes? public class ArrayStack implements Stack { ... } The header of the class becomes, public class ArrayStack<E> implements Stack<E> { ... }
Image of page 44
ArrayStack with Generics What are the required changes for the instance variables? // Instance variables private Object[] elems; private int top; Becomes, public class ArrayStack<E> implements Stack<E> { private E[] elems; private int top; // ...
Image of page 45

Subscribe to view the full document.

ArrayStack with Generics What are the required changes for the constructor? public ArrayStack( int capacity ) { elems = new Object[ capacity ]; top = 0; } A generic array seems appropriate, public ArrayStack( int capacity ) { elems = new E[ capacity ]; top = 0; }
Image of page 46
ArrayStack with Generics public class ArrayStack<E> implements Stack<E> { private E[] elems; private int top; public ArrayStack( int capacity ) { elems = new E[ capacity ]; top = 0; } However, this causes the following compile-time error. ArrayStack.java:11: generic array creation elems = new E[ capacity ]; ^ 1 error
Image of page 47

Subscribe to view the full document.

ArrayStack with Generics For back-compatibility reasons, the creation of generic arrays is not possible, sadly!
Image of page 48
ArrayStack with Generics We still have our problem to solve! Our approach will be. public class ArrayStack<E> implements Stack<E> { private E[] elems; private int top; public ArrayStack( int capacity ) { elems = (E[]) new Object[ capacity ]; top = 0; } // ... which causes a compile-time warning. Note: ArrayStack.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.
Image of page 49

Subscribe to view the full document.

ArrayStack with Generics The warning can be suppressed locally. public class ArrayStack<E> implements Stack<E> { private E[] elems; private int top; @SuppressWarnings( "unchecked" ) public ArrayStack( int capacity ) { elems = (E[]) new Object[ capacity ]; top = 0; } // ... which is a better option than globally suppressing warnings. > javac -Xlint:unchecked ArrayStack.java
Image of page 50
Continuing with the implementation of ArrayStack public void push( E element ) { // pre-condition: the stack is not full // stores the element at position top, then increments top elems[ top++ ] = element; }
Image of page 51

Subscribe to view the full document.

Continuing with the implementation of ArrayStack // Removes and returns the top element of this stack public E pop() { // pre-conditions: ! isEmpty() // decrements top, then access the value E saved = elems[ --top ]; return saved; } } This compiles and runs, but!?
Image of page 52
Continuing with the implementation of ArrayStack elems top (or size) 3 s 0 1 2 3 4 5 6 7 23:00:00 13:00:00 14:30:00
Image of page 53

Subscribe to view the full document.

Continuing with the implementation of ArrayStack elems top (or size) 2 s 0 1 2 3 4 5 6 7 23:00:00 13:00:00 14:30:00
Image of page 54
Continuing with the implementation of ArrayStack elems top (or size) 1 s 0 1 2 3 4 5 6 7 23:00:00 13:00:00 14:30:00
Image of page 55

Subscribe to view the full document.

Continuing with the implementation of ArrayStack elems top (or size) 0 s 0 1 2 3 4 5 6 7 23:00:00 13:00:00 14:30:00
Image of page 56
Image of page 57
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern