Question of the Day

Post on 19-Feb-2016

15 views 0 download

Tags:

description

Question of the Day. Move one matchstick to produce a square. Question of the Day. Move one matchstick to produce a square. CSC 212 – Data Structures. Lecture 12: Generic Types. Generic Types. Starting with 1.5 release, Java uses generics Include type parameters in class definition - PowerPoint PPT Presentation

Transcript of Question of the Day

Question of the Day

Move one matchstick to produce a square

Question of the Day

Move one matchstick to produce a square

LECTURE 12:GENERIC TYPES

CSC 212 – Data Structures

Generic Types

Starting with 1.5 release, Java uses generics

Include type parameters in class definition Like methods, parameters can change each

time Fields independent of types can now be

written

Generic Types

On allocating instance, actual type is specified Must be reference type or String as actual

type Code runs as if were written using that type Type used by instance cannot be changed Type parameter becomes part of variable’s

type

Generics Before & After

Before Generics After Genericspublic class ONode {private Object data;ONode(Object d) { data = d;}Object getData() { return data;}void setData(Object d){ data = d;}

}

public class Node<T> {private T data;public Node(T d) { data = d;}T getData() { return data;}void setData(T d){ data = d;}

}

public class Bag<T, String> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

public class Bag<T, String> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

public class Bag<T, String> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

public class Bag<T> {private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

Writing Generics

Bag<Integer> earth = new Bag<Integer>(4, “The Answer”);public class Bag<T> {

private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

Bag<Integer> earth = new Bag<Integer>(4, “The Answer”);public class Bag {

private Integer data;private String name;

public Bag(Integer d, String newName) { data = d; name = newName;}

public Integer getData() { return data; }

public void setData(Integer d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

For earth, class written as if T were replaced by Integer

Bag<Car> matchbox = new Bag<Car>(Z4, “Dream”);public class Bag<T> {

private T data;private String name;

public Bag(T d, String newName) { data = d; name = newName;}

public T getData() { return data; }

public void setData(T d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

Bag<Car> matchbox = new Bag<Car>(Z4, “Dream”);public class Bag {

private Car data;private String name;

public Bag(Car d, String newName) { data = d; name = newName;}

public Car getData() { return data; }

public void setData(Car d){ data = d; }

public String toString() { return name + “: ” + data.toString();}

}

See Generics Behave

For matchbox, T is Car

This can be at same time T is Integer when for earth

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

Using Generics

Without Generics With GenericsInteger i;Car c;Bag n;

...

n = new Bag(5,“B”);i = ((Integer)n.getData());c = ((Car)n.getData());

n.setData(c);

i = ((Integer)n.getData()); c = ((Car)n.getData());

Integer i;Car c;Bag<Integer> n;Bag<Car> m;

...

n = new Bag<Integer>(5,“B”);i = n.getData();c = n.getData();

n.setData(c);m = new Bag<Car>(c, “B”);

i = m.getData();c = m.getData();

public class Entry<MONKEY, TYPE> { private MONKEY key; private TYPE value; // And more goes here...

}Entry<String, Integer> a;Entry<String> b;Entry<String, String> c;Entry<String, boolean> d;Entry<Car, Boolean> e;

Can Use Multiple Generic Types

public class Entry<MONKEY, TYPE> { private MONKEY key; private TYPE value; // And more goes here...

}Entry<String, Integer> a;Entry<String> b; Did not specify for each typeEntry<String, String> c;Entry<String, boolean> d;Entry<Car, Boolean> e;

Can Use Multiple Generic Types

public class Entry<MONKEY, TYPE> { private MONKEY key; private TYPE value; // And more goes here...

}Entry<String, Integer> a;Entry<String> b; Did not specify for each typeEntry<String, String> c;Entry<String, boolean> d; Not reference typeEntry<Car, Boolean> e;

Can Use Multiple Generic Types

When To Specify Type

Whenever class name used (except constructors) Variable declarations:ArrayList<Integer> hogCount;

Object instantiation:hogCount = new ArrayList<Double>();

Return type for method :private ArrayList<Pig> transport()

Parameter listing:public void cook(ArrayList<Meat> fd)

Used as type parameter:ArrayList<ArrayList<Meat>> bacon;

Type cannot be specified instantiating array Compiler error if type specified during

instantiation Can provide type theory explaining this

problem

Generics Annoyance

Type cannot be specified instantiating array

Can use generics with arrays, but need typecast Only needed once, use generics after instantiation Still checks when compiling, so get most benefits

public class Farm<T> { private ArrayList<Feed>[] troughs; private T[] animals; public Farm() { troughs = (ArrayList<Feed>[])new ArrayList[10]; animals = (T[])new Object[1034821]; }}

Generics Annoyance

Single method or class may not care about type Different than using typecasts to get to

work Only will work if does not need to

instantiate object Do not skip specifying type -- that is BAD

IDEA™ Instead use the generic wildcard ?

In Case of Unknown Type

Single method or class may not care about type Different than using typecasts to get to

work Only will work if does not need to

instantiate object Do not skip specifying type -- that is BAD

IDEA™ Instead use the generic wildcard ?

In Case of Unknown Type

Wildcard in Generic

public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){myList = lst;}

public void printListSize() {System.out.println(myList.size());}

public ArrayList<?> getList() {return myList;}

}

Wildcard in Generic

public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){myList = lst;}

public void printListSize() {System.out.println(myList.size());}

public ArrayList<?> getList() {return myList;}

}

? matches any reference type(and String)

Wildcard in Generic

public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){myList = lst;}

public void printListSize() {System.out.println(myList.size());}

public ArrayList<?> getList() {return myList;}

}

Any ArrayList can be passed in for lst

Wildcard in Generic

public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){myList = lst;}

public void printListSize() {System.out.println(myList.size());}

public ArrayList<?> getList() {return myList;}

}

Can call methods as long asmissing type not important

Wildcard in Generic

public class ListHolder {private ArrayList<?> myList; public void setList(ArrayList<?> lst){myList = lst;}

public void printListSize() {System.out.println(myList.size());}

public ArrayList<?> getList() {return myList;}

}

Legal, but yucky. All type information is lost!

Typecasting Explained

Typecasting Explained

99% of typecasts are incorrect 90% fix missing generic type specification

on variable Eclipse “Quick-Fix” on illegal code is 9% 0.95% instantiate arrays of generic type When using interfaces in an ADT is 0.05%

Typecasting Explained

Your Turn

Get into your groups and complete activity

For Next Lecture

Midterm #1 next Monday for duration of class Will be doing review on Friday, so bring your

questions Object-orientation quiz in class

Wednesday Will be just like quiz in class last week Inheritance, chains, generics & more

covered on quiz As usual, there is weekly assignment on

Angel Due by 5PM tomorrow via Assignment

Submitter Problem #2 graded using provided JUnit

tests No week #5 assignment due to

midterm